github.com/XiaoMi/Gaea@v1.2.5/cc/proxy/proxy_api.go (about)

     1  // Copyright 2019 The Gaea Authors. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package proxy
    16  
    17  import (
    18  	"encoding/json"
    19  
    20  	"github.com/XiaoMi/Gaea/util/requests"
    21  )
    22  
    23  // APIClient api client
    24  type APIClient struct {
    25  	addr     string
    26  	user     string
    27  	password string
    28  }
    29  
    30  // NewAPIClient create api client
    31  func NewAPIClient(addr, user, password string) *APIClient {
    32  	return &APIClient{addr: addr, user: user, password: password}
    33  }
    34  
    35  // PrepareConfig send prepare config
    36  func (c *APIClient) PrepareConfig(name string) error {
    37  	url := c.encodeURL("/api/proxy/config/prepare/%s", name)
    38  	return requests.SendPut(url, c.user, c.password)
    39  }
    40  
    41  // CommitConfig send commit config
    42  func (c *APIClient) CommitConfig(name string) error {
    43  	url := c.encodeURL("/api/proxy/config/commit/%s", name)
    44  	return requests.SendPut(url, c.user, c.password)
    45  }
    46  
    47  // DelNamespace send delete namespace to proxy
    48  func (c *APIClient) DelNamespace(name string) error {
    49  	url := c.encodeURL("/api/proxy/namespace/delete/%s", name)
    50  	return requests.SendPut(url, c.user, c.password)
    51  }
    52  
    53  // GetNamespaceSQLFingerprint return sql fingerprint of specific namespace
    54  func (c *APIClient) GetNamespaceSQLFingerprint(name string) (*SQLFingerprint, error) {
    55  	var reply SQLFingerprint
    56  	url := c.encodeURL("/api/proxy/stats/sessionsqlfingerprint/%s", name)
    57  	resp, err := requests.SendGet(url, c.user, c.password)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	if resp != nil && resp.Body != nil {
    62  		json.Unmarshal(resp.Body, &reply)
    63  	}
    64  	return &reply, err
    65  }
    66  
    67  func (c *APIClient) proxyConfigFingerprint() (string, error) {
    68  	r := ""
    69  	url := c.encodeURL("/api/proxy/config/fingerprint")
    70  	resp, err := requests.SendGet(url, c.user, c.password)
    71  	if err != nil {
    72  		return r, err
    73  	}
    74  	if resp != nil && resp.Body != nil {
    75  		json.Unmarshal(resp.Body, &r)
    76  	}
    77  	return r, err
    78  }
    79  
    80  // Ping ping proxy
    81  func (c *APIClient) Ping() error {
    82  	url := c.encodeURL("/api/proxy/ping")
    83  	_, err := requests.SendGet(url, c.user, c.password)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	return nil
    88  }
    89  
    90  func (c *APIClient) encodeURL(format string, args ...interface{}) string {
    91  	return requests.EncodeURL(c.addr, format, args...)
    92  }