Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
feat: add humanitec user agent header (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Aug 21, 2023
2 parents f216072 + d0d603a commit 56cd6b0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 16 deletions.
16 changes: 12 additions & 4 deletions internal/humanitec_go/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,31 @@ The Apache Software Foundation (http://www.apache.org/).
package client

import (
"fmt"
"net/http"

"github.com/score-spec/score-humanitec/internal/version"
"github.com/sendgrid/rest"
)

var (
ScoreUserAgent = fmt.Sprintf("score-humanitec/%s", version.Version)
)

type apiClient struct {
baseUrl string
token string
baseUrl string
token string
humanitecUserAgent string

client *rest.Client
}

// NewClient constructs new Humanitec API client.
func NewClient(url, token string, httpClient *http.Client) (Client, error) {
return &apiClient{
baseUrl: url,
token: token,
baseUrl: url,
token: token,
humanitecUserAgent: fmt.Sprintf("app %s; sdk %s", ScoreUserAgent, ScoreUserAgent),

client: &rest.Client{
HTTPClient: httpClient,
Expand Down
14 changes: 8 additions & 6 deletions internal/humanitec_go/client/deltas.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ func (api *apiClient) CreateDelta(ctx context.Context, orgID, appID string, delt
Method: http.MethodPost,
BaseURL: api.baseUrl + apiPath,
Headers: map[string]string{
"Authorization": "Bearer " + api.token,
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + api.token,
"Content-Type": "application/json",
"Accept": "application/json",
"Humanitec-User-Agent": api.humanitecUserAgent,
},
Body: data,
}
Expand Down Expand Up @@ -76,9 +77,10 @@ func (api *apiClient) UpdateDelta(ctx context.Context, orgID string, appID strin
Method: http.MethodPatch,
BaseURL: api.baseUrl + apiPath,
Headers: map[string]string{
"Authorization": "Bearer " + api.token,
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + api.token,
"Content-Type": "application/json",
"Accept": "application/json",
"Humanitec-User-Agent": api.humanitecUserAgent,
},
Body: buf.Bytes(),
}
Expand Down
4 changes: 3 additions & 1 deletion internal/humanitec_go/client/deltas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestCreateDelta(t *testing.T) {
Response: []byte(`{
"id": "qwe...rty",
"metadata": { "env_id": "test", "name": "Test delta" },
"modules": {
"modules": {
"add": { "module-01": { "image": "busybox", "variables": { "TEST": "<a>" } } }
}
}`),
Expand Down Expand Up @@ -111,6 +111,7 @@ func TestCreateDelta(t *testing.T) {
assert.Equal(t, []string{"Bearer " + apiToken}, r.Header["Authorization"])
assert.Equal(t, []string{"application/json"}, r.Header["Accept"])
assert.Equal(t, []string{"application/json"}, r.Header["Content-Type"])
assert.Equal(t, []string{"app score-humanitec/0.0.0; sdk score-humanitec/0.0.0"}, r.Header["Humanitec-User-Agent"])

if tt.Data != nil {
var body humanitec.CreateDeploymentDeltaRequest
Expand Down Expand Up @@ -168,6 +169,7 @@ func TestUpdateDelta_success(t *testing.T) {
assert.Equal(t, []string{"Bearer " + apiToken}, r.Header["Authorization"])
assert.Equal(t, []string{"application/json"}, r.Header["Accept"])
assert.Equal(t, []string{"application/json"}, r.Header["Content-Type"])
assert.Equal(t, []string{"app score-humanitec/0.0.0; sdk score-humanitec/0.0.0"}, r.Header["Humanitec-User-Agent"])
var body []*humanitec.UpdateDeploymentDeltaRequest
var dec = json.NewDecoder(r.Body)
assert.NoError(t, dec.Decode(&body))
Expand Down
7 changes: 4 additions & 3 deletions internal/humanitec_go/client/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ func (api *apiClient) StartDeployment(ctx context.Context, orgID, appID, envID s
Method: http.MethodPost,
BaseURL: api.baseUrl + apiPath,
Headers: map[string]string{
"Authorization": "Bearer " + api.token,
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + api.token,
"Content-Type": "application/json",
"Accept": "application/json",
"Humanitec-User-Agent": api.humanitecUserAgent,
},
Body: data,
}
Expand Down
1 change: 1 addition & 0 deletions internal/humanitec_go/client/deployments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func TestStartDeployment(t *testing.T) {
assert.Equal(t, []string{"Bearer " + apiToken}, r.Header["Authorization"])
assert.Equal(t, []string{"application/json"}, r.Header["Accept"])
assert.Equal(t, []string{"application/json"}, r.Header["Content-Type"])
assert.Equal(t, []string{"app score-humanitec/0.0.0; sdk score-humanitec/0.0.0"}, r.Header["Humanitec-User-Agent"])

if tt.Data != nil {
var body humanitec.StartDeploymentRequest
Expand Down
5 changes: 3 additions & 2 deletions internal/humanitec_go/client/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func (api *apiClient) ListResourceTypes(ctx context.Context, orgID string) ([]hu
Method: http.MethodGet,
BaseURL: api.baseUrl + apiPath,
Headers: map[string]string{
"Authorization": "Bearer " + api.token,
"Accept": "application/json",
"Authorization": "Bearer " + api.token,
"Accept": "application/json",
"Humanitec-User-Agent": api.humanitecUserAgent,
},
}

Expand Down
1 change: 1 addition & 0 deletions internal/humanitec_go/client/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func TestListResourceTypes(t *testing.T) {
}
assert.Equal(t, []string{"Bearer " + apiToken}, r.Header["Authorization"])
assert.Equal(t, []string{"application/json"}, r.Header["Accept"])
assert.Equal(t, []string{"app score-humanitec/0.0.0; sdk score-humanitec/0.0.0"}, r.Header["Humanitec-User-Agent"])

w.WriteHeader(tt.StatusCode)
if len(tt.Response) > 0 {
Expand Down

0 comments on commit 56cd6b0

Please sign in to comment.