github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/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  	routerGroupEndpoint string
    17  
    18  	connection Connection
    19  	router     *rata.RequestGenerator
    20  	userAgent  string
    21  }
    22  
    23  // Config allows the Client to be configured
    24  type Config struct {
    25  	// AppName is the name of the application/process using the client.
    26  	AppName string
    27  
    28  	// AppVersion is the version of the application/process using the client.
    29  	AppVersion string
    30  
    31  	// ConnectionConfig is the configuration for the client connection.
    32  	ConnectionConfig
    33  
    34  	// RoutingEndpoint is the url of the router API.
    35  	RoutingEndpoint string
    36  
    37  	// Wrappers that apply to the client connection.
    38  	Wrappers []ConnectionWrapper
    39  }
    40  
    41  // NewClient returns a new Router Client.
    42  func NewClient(config Config) *Client {
    43  	userAgent := fmt.Sprintf("%s/%s (%s; %s %s)",
    44  		config.AppName,
    45  		config.AppVersion,
    46  		runtime.Version(),
    47  		runtime.GOARCH,
    48  		runtime.GOOS,
    49  	)
    50  
    51  	client := Client{
    52  		userAgent:  userAgent,
    53  		router:     rata.NewRequestGenerator(config.RoutingEndpoint, internal.APIRoutes),
    54  		connection: NewConnection(config.ConnectionConfig),
    55  	}
    56  
    57  	for _, wrapper := range config.Wrappers {
    58  		client.connection = wrapper.Wrap(client.connection)
    59  	}
    60  
    61  	return &client
    62  }