github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/api/v2/processor.go (about)

     1  // Copyright 2023 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  	"context"
    18  	"fmt"
    19  
    20  	v2 "github.com/pingcap/tiflow/cdc/api/v2"
    21  	"github.com/pingcap/tiflow/pkg/api/internal/rest"
    22  )
    23  
    24  // ProcessorsGetter has a method to return a ProcessorInterface.
    25  type ProcessorsGetter interface {
    26  	Processors() ProcessorInterface
    27  }
    28  
    29  // ProcessorInterface has methods to work with Processor items.
    30  // We can also mock the processor operations by implement this interface.
    31  type ProcessorInterface interface {
    32  	Get(ctx context.Context, namespace string, changefeedID, captureID string) (*v2.ProcessorDetail, error)
    33  	List(ctx context.Context) ([]v2.ProcessorCommonInfo, error)
    34  }
    35  
    36  // processors implements ProcessorInterface.
    37  type processors struct {
    38  	client rest.CDCRESTInterface
    39  }
    40  
    41  // newProcessors returns processors.
    42  func newProcessors(c *APIV2Client) *processors {
    43  	return &processors{
    44  		client: c.RESTClient(),
    45  	}
    46  }
    47  
    48  // List returns the list of processors.
    49  func (p *processors) List(ctx context.Context) ([]v2.ProcessorCommonInfo, error) {
    50  	result := &v2.ListResponse[v2.ProcessorCommonInfo]{}
    51  	err := p.client.Get().
    52  		WithURI("processors").
    53  		Do(ctx).
    54  		Into(result)
    55  	return result.Items, err
    56  }
    57  
    58  // Get gets the processor with given `changefeedID` and `captureID`.
    59  func (p *processors) Get(
    60  	ctx context.Context,
    61  	namespace,
    62  	changefeedID,
    63  	captureID string,
    64  ) (*v2.ProcessorDetail, error) {
    65  	result := &v2.ProcessorDetail{}
    66  	u := fmt.Sprintf("processors/%s/%s?namespace=%s", changefeedID, captureID, namespace)
    67  	err := p.client.Get().
    68  		WithURI(u).
    69  		Do(ctx).
    70  		Into(result)
    71  	return result, err
    72  }