Skip to content

Commit

Permalink
Add basic util to request api
Browse files Browse the repository at this point in the history
  • Loading branch information
amishas157 committed Nov 14, 2024
1 parent 7137253 commit 483f28f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
34 changes: 34 additions & 0 deletions utils/apiclient/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package apiclient

import (
"fmt"
"net/http"
"net/url"

"github.com/pkg/errors"
)

func (c *APIClient) getRequest(endpoint string, queryParams url.Values) error {

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

View workflow job for this annotation

GitHub Actions / check (ubuntu-22.04, 1.22.1)

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

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return errors.Wrap(err, "http GET request failed")
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("API request failed with status %d", resp.StatusCode)
}

return nil
}

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

Check failure on line 32 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())
}
22 changes: 22 additions & 0 deletions utils/apiclient/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package apiclient

import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_url(t *testing.T) {
c := &APIClient{
BaseURL: "https://stellar.org",
}

qstr := url.Values{}
qstr.Add("type", "forward")
qstr.Add("federation_type", "bank_account")
qstr.Add("swift", "BOPBPHMM")
qstr.Add("acct", "2382376")
furl := c.url("federation", qstr)
assert.Equal(t, "https://stellar.org/federation?acct=2382376&federation_type=bank_account&swift=BOPBPHMM&type=forward", furl)
}
6 changes: 6 additions & 0 deletions utils/apiclient/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package apiclient

type APIClient struct {
BaseURL string
AuthToken string
}

0 comments on commit 483f28f

Please sign in to comment.