github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/api/internal/rest/client.go (about)

     1  // Copyright 2021 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package rest
    15  
    16  import (
    17  	"net/url"
    18  	"strings"
    19  
    20  	"github.com/pingcap/tiflow/pkg/httputil"
    21  )
    22  
    23  // HTTPMethod represents HTTP method.
    24  type HTTPMethod int
    25  
    26  // Valid HTTP methods.
    27  const (
    28  	HTTPMethodPost = iota + 1
    29  	HTTPMethodPut
    30  	HTTPMethodGet
    31  	HTTPMethodDelete
    32  )
    33  
    34  // String implements Stringer.String.
    35  func (h HTTPMethod) String() string {
    36  	switch h {
    37  	case HTTPMethodPost:
    38  		return "POST"
    39  	case HTTPMethodPut:
    40  		return "PUT"
    41  	case HTTPMethodGet:
    42  		return "GET"
    43  	case HTTPMethodDelete:
    44  		return "DELETE"
    45  	default:
    46  		return "unknown"
    47  	}
    48  }
    49  
    50  // CDCRESTInterface includes a set of operations to interact with TiCDC RESTful apis.
    51  type CDCRESTInterface interface {
    52  	Method(method HTTPMethod) *Request
    53  	Post() *Request
    54  	Put() *Request
    55  	Get() *Request
    56  	Delete() *Request
    57  }
    58  
    59  // BasicAuth holds the basic authentication information.
    60  type BasicAuth struct {
    61  	User     string
    62  	Password string
    63  }
    64  
    65  // CDCRESTClient defines a TiCDC RESTful client
    66  type CDCRESTClient struct {
    67  	// base is the root URL for all invocations of the client.
    68  	base      *url.URL
    69  	basicAuth BasicAuth
    70  	params    url.Values
    71  
    72  	// versionedAPIPath is a http url prefix with api version. eg. /api/v1.
    73  	versionedAPIPath string
    74  
    75  	// Client is a wrapped http client.
    76  	Client *httputil.Client
    77  }
    78  
    79  // NewCDCRESTClient creates a new CDCRESTClient.
    80  func NewCDCRESTClient(
    81  	baseURL *url.URL,
    82  	versionedAPIPath string,
    83  	client *httputil.Client,
    84  	basicAuth BasicAuth,
    85  	params url.Values,
    86  ) (*CDCRESTClient, error) {
    87  	if !strings.HasSuffix(baseURL.Path, "/") {
    88  		baseURL.Path += "/"
    89  	}
    90  	baseURL.RawQuery = ""
    91  	baseURL.Fragment = ""
    92  
    93  	return &CDCRESTClient{
    94  		base:             baseURL,
    95  		basicAuth:        basicAuth,
    96  		versionedAPIPath: versionedAPIPath,
    97  		Client:           client,
    98  		params:           params,
    99  	}, nil
   100  }
   101  
   102  // Method begins a request with a http method (GET, POST, PUT, DELETE).
   103  func (c *CDCRESTClient) Method(method HTTPMethod) *Request {
   104  	return NewRequest(c).WithMethod(method)
   105  }
   106  
   107  // Post begins a POST request. Short for c.Method(HTTPMethodPost).
   108  func (c *CDCRESTClient) Post() *Request {
   109  	return c.Method(HTTPMethodPost)
   110  }
   111  
   112  // Put begins a PUT request. Short for c.Method(HTTPMethodPut).
   113  func (c *CDCRESTClient) Put() *Request {
   114  	return c.Method(HTTPMethodPut)
   115  }
   116  
   117  // Delete begins a DELETE request. Short for c.Method(HTTPMethodDelete).
   118  func (c *CDCRESTClient) Delete() *Request {
   119  	return c.Method(HTTPMethodDelete)
   120  }
   121  
   122  // Get begins a GET request. Short for c.Method(HTTPMethodGet).
   123  func (c *CDCRESTClient) Get() *Request {
   124  	return c.Method(HTTPMethodGet)
   125  }