github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/api/v2/api_client.go (about) 1 // Copyright 2022 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 v2 15 16 import ( 17 "net/url" 18 19 "github.com/pingcap/errors" 20 "github.com/pingcap/tiflow/pkg/api/internal/rest" 21 "github.com/pingcap/tiflow/pkg/security" 22 ) 23 24 // APIV2Interface is an abstraction for TiCDC capture/changefeed/processor operations. 25 // We can create a fake api client which mocks TiCDC operations by implement this interface. 26 type APIV2Interface interface { 27 RESTClient() rest.CDCRESTInterface 28 ChangefeedsGetter 29 TsoGetter 30 UnsafeGetter 31 StatusGetter 32 CapturesGetter 33 ProcessorsGetter 34 } 35 36 // APIV2Client implements APIV1Interface and it is used to interact with cdc owner http api. 37 type APIV2Client struct { 38 restClient rest.CDCRESTInterface 39 } 40 41 // RESTClient returns a RESTClient that is used to communicate with owner api 42 // by this client implementation. 43 func (c *APIV2Client) RESTClient() rest.CDCRESTInterface { 44 if c == nil { 45 return nil 46 } 47 return c.restClient 48 } 49 50 // Tso returns a TsoInterface to communicate with cdc api 51 func (c *APIV2Client) Tso() TsoInterface { 52 if c == nil { 53 return nil 54 } 55 return newTso(c) 56 } 57 58 // Unsafe returns a UnsafeInterface to communicate with cdc api 59 func (c *APIV2Client) Unsafe() UnsafeInterface { 60 if c == nil { 61 return nil 62 } 63 return newUnsafe(c) 64 } 65 66 // Changefeeds returns a ChangefeedInterface with cdc api 67 func (c *APIV2Client) Changefeeds() ChangefeedInterface { 68 if c == nil { 69 return nil 70 } 71 return newChangefeeds(c) 72 } 73 74 // Status returns a StatusInterface to communicate with cdc api 75 func (c *APIV2Client) Status() StatusInterface { 76 if c == nil { 77 return nil 78 } 79 return newStatus(c) 80 } 81 82 // Captures returns a CaptureInterface which abstracts capture operations. 83 func (c *APIV2Client) Captures() CaptureInterface { 84 return newCaptures(c) 85 } 86 87 // Processors returns a ProcessorInterface abstracting processor operations. 88 func (c *APIV2Client) Processors() ProcessorInterface { 89 if c == nil { 90 return nil 91 } 92 return newProcessors(c) 93 } 94 95 // NewAPIClient creates a new APIV1Client. 96 func NewAPIClient(serverAddr string, credential *security.Credential, values url.Values) (*APIV2Client, error) { 97 c := &rest.Config{} 98 c.APIPath = "/api" 99 c.Version = "v2" 100 c.Host = serverAddr 101 c.Credential = credential 102 c.Values = values 103 client, err := rest.CDCRESTClientFromConfig(c) 104 if err != nil { 105 return nil, errors.Trace(err) 106 } 107 108 return &APIV2Client{client}, nil 109 }