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

feat: add humanitec user agent header #52

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/command/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func init() {
deltaCmd.MarkFlagRequired("env")

deltaCmd.Flags().StringArrayVarP(&overrideParams, "property", "p", nil, "Overrides selected property value")
deltaCmd.Flags().StringVar(&message, "message", "m", messageDefault, "Message")
deltaCmd.Flags().StringVarP(&message, "message", "m", messageDefault, "Message")

deltaCmd.Flags().BoolVar(&deploy, "deploy", false, "Trigger a new delta deployment at the end")
deltaCmd.Flags().BoolVar(&retry, "retry", false, "Retry deployments when a deployment is currently in progress")
Expand Down
2 changes: 1 addition & 1 deletion internal/command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func init() {
runCmd.MarkFlagRequired("env")

runCmd.Flags().StringArrayVarP(&overrideParams, "property", "p", nil, "Overrides selected property value")
runCmd.Flags().StringVar(&message, "message", "m", messageDefault, "Message")
runCmd.Flags().StringVarP(&message, "message", "m", messageDefault, "Message")

runCmd.Flags().BoolVar(&skipValidation, "skip-validation", false, "DEPRECATED: Disables Score file schema validation.")
runCmd.Flags().BoolVar(&verbose, "verbose", false, "Enable diagnostic messages (written to STDERR)")
Expand Down
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