-
Notifications
You must be signed in to change notification settings - Fork 500
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7137253
commit 483f28f
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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 { | ||
return fmt.Sprintf("%s/%s?%s", c.BaseURL, endpoint, qstr.Encode()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package apiclient | ||
|
||
type APIClient struct { | ||
BaseURL string | ||
AuthToken string | ||
} |