-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add outbound-ips implementation
ReadMe's API now includes an endpoint for retrieving a list of their outbound IP addresses for use with webhooks and the API explorer. This is a simple single GET call with a very simple data model. See https://docs.readme.com/main/reference/getoutboundips for more information.
- Loading branch information
Showing
5 changed files
with
213 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,47 @@ | ||
package readme | ||
|
||
// OutboundIPEndpoint is the ReadMe API URL endpoint for retrieving ReadMe's outbound IP addresses. | ||
const OutboundIPEndpoint = "/outbound-ips" | ||
|
||
// OutboundIPService is an interface for using the outbound IPs endpoint of the ReadMe.com API. | ||
// | ||
// API Reference: https://docs.readme.com/main/reference/getoutboundips | ||
type OutboundIPService interface { | ||
// Get all of ReadMe’s IP addresses used for outbound webhook requests and the “Try It!” button on the API Explorer. | ||
// | ||
// Although ReadMe’s outbound IP addresses may change, the IPs in this API | ||
// response will be valid for at least 7 days. If you configure your API or | ||
// webhooks to limit access based on these IPs, you should refresh the IP list | ||
// from this endpoint weekly. | ||
// | ||
// API Reference: https://docs.readme.com/main/reference/getproject | ||
Get() ([]OutboundIP, *APIResponse, error) | ||
} | ||
|
||
// OutboundIPClient handles communication with the OutboundIP related methods of the ReadMe.com API. | ||
type OutboundIPClient struct { | ||
client *Client | ||
} | ||
|
||
// OutboundIP represents the response from the ReadMe API when returning outbound IP addresses. | ||
type OutboundIP struct { | ||
IPAddress string `json:"ipAddress"` | ||
} | ||
|
||
var _ OutboundIPService = &OutboundIPClient{} | ||
|
||
// Get all of ReadMe’s IP addresses used for outbound webhook requests and the “Try It!” button on the API Explorer. | ||
// | ||
// API Reference: https://docs.readme.com/main/reference/getproject | ||
func (c OutboundIPClient) Get() ([]OutboundIP, *APIResponse, error) { | ||
ipList := []OutboundIP{} | ||
apiResponse, err := c.client.APIRequest(&APIRequest{ | ||
Method: "GET", | ||
Endpoint: OutboundIPEndpoint, | ||
UseAuth: false, | ||
OkStatusCode: []int{200}, | ||
Response: &ipList, | ||
}) | ||
|
||
return ipList, apiResponse, err | ||
} |
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,47 @@ | ||
package readme_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/h2non/gock" | ||
"github.com/liveoaklabs/readme-api-go-client/tests/testdata" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_OutboundIPs_Get(t *testing.T) { | ||
t.Run("when API responds with 200", func(t *testing.T) { | ||
// Arrange | ||
expect := testdata.OutboundIPs | ||
gock.New(TestClient.APIURL). | ||
Get("/outbound-ips"). | ||
Reply(200). | ||
JSON(expect) | ||
defer gock.Off() | ||
|
||
// Act | ||
got, _, err := TestClient.OutboundIP.Get() | ||
|
||
// Assert | ||
assert.NoError(t, err, "it does not return an error") | ||
assert.Equal(t, expect, got, "it returns expected Project struct") | ||
assert.True(t, gock.IsDone(), "it makes the expected API call") | ||
}) | ||
|
||
t.Run("when API response cannot be parsed", func(t *testing.T) { | ||
// Arrange | ||
gock.New(TestClient.APIURL). | ||
Get("/outbound-ips"). | ||
Reply(200). | ||
JSON(`[{"invalid":invalid"}]`) | ||
defer gock.Off() | ||
|
||
// Act | ||
_, _, err := TestClient.OutboundIP.Get() | ||
|
||
// Assert | ||
assert.ErrorContains(t, err, | ||
"unable to parse API response: invalid character", | ||
"it returns the expected error") | ||
assert.True(t, gock.IsDone(), "it makes the expected API call") | ||
}) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
package testdata | ||
|
||
import "github.com/liveoaklabs/readme-api-go-client/readme" | ||
|
||
var OutboundIPs = []readme.OutboundIP{ | ||
{ | ||
IPAddress: "127.0.0.1", | ||
}, | ||
{ | ||
IPAddress: "10.0.1.2", | ||
}, | ||
{ | ||
IPAddress: "10.10.9.8", | ||
}, | ||
} |