...

Source file src/edge-infra.dev/pkg/edge/api/client/options.go

Documentation: edge-infra.dev/pkg/edge/api/client

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/url"
     7  	"time"
     8  
     9  	"github.com/shurcooL/graphql"
    10  )
    11  
    12  // Option enables options to be set for the Edge Client.
    13  type Option = func(c *EdgeClient)
    14  
    15  // WithGraphqlClient sets the specified GraphQL client for the Edge Client.
    16  func WithGraphqlClient(cl *graphql.Client) Option {
    17  	return func(c *EdgeClient) {
    18  		c.Client = cl
    19  	}
    20  }
    21  
    22  // WithHTTPClient sets the specified http client for the Edge Client.
    23  // Because this overrides the default http client that Edge Client provides
    24  // You will have to also implement and specificy the cookie jar yourself
    25  // Example:
    26  //
    27  //	jar, err := cookiejar.New(&cookiejar.Options{})
    28  //	&http.Client{
    29  //		Jar:     c.jar,
    30  //	}
    31  func WithHTTPClient(cl *http.Client) Option {
    32  	return func(c *EdgeClient) {
    33  		c.HTTPClient = cl
    34  	}
    35  }
    36  
    37  // WithUserAgent sets and overrides the default http user agent header for the Edge Client.
    38  func WithUserAgent(userAgent string) Option {
    39  	return func(c *EdgeClient) {
    40  		c.userAgent = userAgent
    41  	}
    42  }
    43  
    44  // WithVersion sets the edge version header value for the Edge Client.
    45  func WithVersion(version string) Option {
    46  	return func(c *EdgeClient) {
    47  		c.version = version
    48  	}
    49  }
    50  
    51  // WithBaseURL sets the api url for the Edge Client to make requests.
    52  // The default value is https://dev1.edge-preprod.dev/api/v2
    53  func WithBaseURL(apiURL string) Option {
    54  	return func(c *EdgeClient) {
    55  		baseURL, err := url.Parse(apiURL)
    56  		if err != nil {
    57  			panic(err)
    58  		}
    59  		c.BaseURL = baseURL
    60  	}
    61  }
    62  
    63  // WithTotp sets the totp token to use for requests.
    64  // It is very discouraged to use multiple authentication methods.
    65  func WithTotp(token string) Option {
    66  	return func(c *EdgeClient) {
    67  		c.totpToken = token
    68  		c.headers[authorizationHeader] = fmt.Sprintf("%s %s", totpToken, token)
    69  	}
    70  }
    71  
    72  // WithBearerToken sets the jwt bearer token to use for requests.
    73  // It is very discouraged to use multiple authentication methods.
    74  func WithBearerToken(token string) Option {
    75  	return func(c *EdgeClient) {
    76  		c.bearerToken = token
    77  		c.headers[authorizationHeader] = fmt.Sprintf("%s %s", bearerToken, token)
    78  	}
    79  }
    80  
    81  // WithCredentials sets the username, password and organization to use for the authentication request.
    82  // It is very discouraged to use multiple authentication methods.
    83  func WithCredentials(username, password, organization string) Option {
    84  	return func(c *EdgeClient) {
    85  		c.loginCredentials = &LoginRequest{
    86  			Username:     username,
    87  			Password:     password,
    88  			Organization: organization,
    89  		}
    90  	}
    91  }
    92  
    93  // WithTimeout sets and overrides the default http timeout for the Edge Client.
    94  func WithTimeout(timeout time.Duration) Option {
    95  	return func(c *EdgeClient) {
    96  		c.timeout = timeout
    97  	}
    98  }
    99  

View as plain text