github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/router/client.go (about)

     1  // Package router is a GoLang library that interacts with CloudFoundry Go Router
     2  package router
     3  
     4  import (
     5  	"fmt"
     6  	"runtime"
     7  
     8  	"code.cloudfoundry.org/cli/api/router/internal"
     9  
    10  	"github.com/tedsuo/rata"
    11  )
    12  
    13  // Client is a client that can be used to talk to a Cloud Controller's V2
    14  // Endpoints.
    15  type Client struct {
    16  	connection Connection
    17  	router     *rata.RequestGenerator
    18  	userAgent  string
    19  }
    20  
    21  // Config allows the Client to be configured
    22  type Config struct {
    23  	// AppName is the name of the application/process using the client.
    24  	AppName string
    25  
    26  	// AppVersion is the version of the application/process using the client.
    27  	AppVersion string
    28  
    29  	// ConnectionConfig is the configuration for the client connection.
    30  	ConnectionConfig
    31  
    32  	// RoutingEndpoint is the url of the router API.
    33  	RoutingEndpoint string
    34  
    35  	// Wrappers that apply to the client connection.
    36  	Wrappers []ConnectionWrapper
    37  }
    38  
    39  // NewClient returns a new Router Client.
    40  func NewClient(config Config) *Client {
    41  	userAgent := fmt.Sprintf("%s/%s (%s; %s %s)",
    42  		config.AppName,
    43  		config.AppVersion,
    44  		runtime.Version(),
    45  		runtime.GOARCH,
    46  		runtime.GOOS,
    47  	)
    48  
    49  	client := Client{
    50  		userAgent:  userAgent,
    51  		router:     rata.NewRequestGenerator(config.RoutingEndpoint, internal.APIRoutes),
    52  		connection: NewConnection(config.ConnectionConfig),
    53  	}
    54  
    55  	for _, wrapper := range config.Wrappers {
    56  		client.connection = wrapper.Wrap(client.connection)
    57  	}
    58  
    59  	return &client
    60  }