Skip to content

Commit

Permalink
Add client version to user agent (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie authored Dec 11, 2024
1 parent e1450a6 commit 6e137c0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 4 additions & 2 deletions internal/artieclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ func (he HttpError) Error() string {
type Client struct {
endpoint string
apiKey string
version string
}

func New(endpoint string, apiKey string) (Client, error) {
func New(endpoint string, apiKey string, version string) (Client, error) {
if !strings.HasPrefix(apiKey, "arsk_") {
return Client{}, fmt.Errorf("artie-client: api key is malformed (should start with arsk_)")
}

return Client{endpoint: endpoint, apiKey: apiKey}, nil
return Client{endpoint: endpoint, apiKey: apiKey, version: version}, nil
}

func buildError(resp *http.Response) error {
Expand Down Expand Up @@ -78,6 +79,7 @@ func (c Client) makeRequest(ctx context.Context, method string, path string, bod
return fmt.Errorf("artie-client: failed to create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiKey))
req.Header.Set("User-Agent", "terraform-provider-artie/"+c.version)

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ type ArtieProviderModel struct {
type ArtieProviderData struct {
Endpoint string
APIKey string
version string
}

func (a ArtieProviderData) NewClient() (artieclient.Client, error) {
return artieclient.New(a.Endpoint, a.APIKey)
return artieclient.New(a.Endpoint, a.APIKey, a.version)
}

func (p *ArtieProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
Expand Down Expand Up @@ -75,6 +76,7 @@ func (p *ArtieProvider) Configure(ctx context.Context, req provider.ConfigureReq
providerData := ArtieProviderData{
Endpoint: endpoint,
APIKey: configData.APIKey.ValueString(),
version: p.version,
}

resp.DataSourceData = providerData
Expand Down

0 comments on commit 6e137c0

Please sign in to comment.