Skip to content

Commit

Permalink
Add method for basic auth and api key auth. Also support headers
Browse files Browse the repository at this point in the history
  • Loading branch information
amishas157 committed Nov 15, 2024
1 parent 7fc2c62 commit 525079f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
60 changes: 54 additions & 6 deletions utils/apiclient/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apiclient

import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -10,17 +11,20 @@ import (
"github.com/pkg/errors"
)

func (c *APIClient) getRequest(endpoint string, queryParams url.Values) (interface{}, error) {
client := c.HTTP
if client == nil {
client = &http.Client{}
}

func (c *APIClient) createRequestBody(endpoint string, queryParams url.Values) (*http.Request, error) {

Check failure on line 14 in utils/apiclient/client.go

View workflow job for this annotation

GitHub Actions / check (ubuntu-22.04, 1.22.1)

func (*APIClient).createRequestBody is unused (U1000)
fullURL := c.url(endpoint, queryParams)
req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
return nil, errors.Wrap(err, "http GET request creation failed")
}
return req, nil
}

func (c *APIClient) callAPI(req *http.Request) (interface{}, error) {

Check failure on line 23 in utils/apiclient/client.go

View workflow job for this annotation

GitHub Actions / check (ubuntu-22.04, 1.22.1)

func (*APIClient).callAPI is unused (U1000)
client := c.HTTP
if client == nil {
client = &http.Client{}
}

resp, err := client.Do(req)
if err != nil {
Expand All @@ -45,6 +49,50 @@ func (c *APIClient) getRequest(endpoint string, queryParams url.Values) (interfa
return result, nil
}

func setHeaders(req *http.Request, args map[string]interface{}) {

Check failure on line 52 in utils/apiclient/client.go

View workflow job for this annotation

GitHub Actions / check (ubuntu-22.04, 1.22.1)

func setHeaders is unused (U1000)
for key, value := range args {
strValue, ok := value.(string)
if !ok {
fmt.Printf("Skipping non-string value for header %s\n", key)
continue
}

req.Header.Set(key, strValue)
}
}

func setAuthHeaders(req *http.Request, authType string, args map[string]interface{}) error {

Check failure on line 64 in utils/apiclient/client.go

View workflow job for this annotation

GitHub Actions / check (ubuntu-22.04, 1.22.1)

func setAuthHeaders is unused (U1000)
switch authType {
case "basic":
username, ok := args["username"].(string)
if !ok {
return fmt.Errorf("missing or invalid username")
}
password, ok := args["password"].(string)
if !ok {
return fmt.Errorf("missing or invalid password")
}

authHeader := "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
setHeaders(req, map[string]interface{}{
"Authorization": authHeader,
})

case "api_key":
apiKey, ok := args["api_key"].(string)
if !ok {
return fmt.Errorf("missing or invalid API key")
}
setHeaders(req, map[string]interface{}{
"Authorization": apiKey,
})

default:
return fmt.Errorf("unsupported auth type: %s", authType)
}
return nil
}

func (c *APIClient) url(endpoint string, qstr url.Values) string {

Check failure on line 96 in utils/apiclient/client.go

View workflow job for this annotation

GitHub Actions / check (ubuntu-22.04, 1.22.1)

func (*APIClient).url is unused (U1000)
return fmt.Sprintf("%s/%s?%s", c.BaseURL, endpoint, qstr.Encode())
}
12 changes: 10 additions & 2 deletions utils/apiclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Test_url(t *testing.T) {
assert.Equal(t, "https://stellar.org/federation?acct=2382376&federation_type=bank_account&swift=BOPBPHMM&type=forward", furl)
}

func Test_getRequest(t *testing.T) {
func Test_callAPI(t *testing.T) {
friendbotFundResponse := `{"key": "value"}`

hmock := httptest.NewClient()
Expand All @@ -41,10 +41,18 @@ func Test_getRequest(t *testing.T) {
qstr.Add("swift", "BOPBPHMM")
qstr.Add("acct", "2382376")

result, err := c.getRequest("federation", qstr)
req, err := c.createRequestBody("federation", qstr)
if err != nil {
t.Fatal(err)
}
setAuthHeaders(req, "api_key", map[string]interface{}{"api_key": "test_api_key"})
assert.Equal(t, "test_api_key", req.Header.Get("Authorization"))

result, err := c.callAPI(req)
if err != nil {
t.Fatal(err)
}

expected := map[string]interface{}{"key": "value"}
assert.Equal(t, expected, result)
}
5 changes: 2 additions & 3 deletions utils/apiclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type HTTP interface {
}

type APIClient struct {
BaseURL string
AuthToken string
HTTP HTTP
BaseURL string
HTTP HTTP
}

0 comments on commit 525079f

Please sign in to comment.