generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented /v1/discount-redemptions APIs
- Loading branch information
1 parent
3a93760
commit 6599d2b
Showing
8 changed files
with
306 additions
and
26 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
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
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
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,28 @@ | ||
package client | ||
|
||
import "time" | ||
|
||
// DiscountRedemptionAttributes is a record of a discount being applied to an order. | ||
type DiscountRedemptionAttributes struct { | ||
DiscountID int `json:"discount_id"` | ||
OrderID int `json:"order_id"` | ||
DiscountName string `json:"discount_name"` | ||
DiscountCode string `json:"discount_code"` | ||
DiscountAmount int `json:"discount_amount"` | ||
DiscountAmountType string `json:"discount_amount_type"` | ||
Amount int `json:"amount"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
} | ||
|
||
// ApiResponseRelationshipsDiscountRedemption relationships of a subscription invoice | ||
type ApiResponseRelationshipsDiscountRedemption struct { | ||
Discount ApiResponseLinks `json:"discount"` | ||
Order ApiResponseLinks `json:"order"` | ||
} | ||
|
||
// DiscountRedemptionApiResponse is the api response for one subscription invoice | ||
type DiscountRedemptionApiResponse = ApiResponse[DiscountRedemptionAttributes, ApiResponseRelationshipsDiscountRedemption] | ||
|
||
// DiscountRedemptionsApiResponse is the api response for a list of subscription invoices. | ||
type DiscountRedemptionsApiResponse = ApiResponseList[DiscountRedemptionAttributes, ApiResponseRelationshipsDiscountRedemption] |
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,44 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
// DiscountRedemptionsService is the API client for the `/v1/discount-redemptions` endpoint | ||
type DiscountRedemptionsService service | ||
|
||
// Get returns the discount redemption with the given ID. | ||
// | ||
// https://docs.lemonsqueezy.com/api/discount-redemptions#retrieve-a-discount-redemption | ||
func (service *DiscountRedemptionsService) Get(ctx context.Context, invoiceID string) (*DiscountRedemptionApiResponse, *Response, error) { | ||
response, err := service.client.do(ctx, http.MethodGet, "/v1/discount-redemptions/"+invoiceID) | ||
if err != nil { | ||
return nil, response, err | ||
} | ||
|
||
redemption := new(DiscountRedemptionApiResponse) | ||
if err = json.Unmarshal(*response.Body, redemption); err != nil { | ||
return nil, response, err | ||
} | ||
|
||
return redemption, response, nil | ||
} | ||
|
||
// List a paginated list of discount redemptions. | ||
// | ||
// https://docs.lemonsqueezy.com/api/discount-redemptions#list-all-discount-redemptions | ||
func (service *DiscountRedemptionsService) List(ctx context.Context) (*DiscountRedemptionsApiResponse, *Response, error) { | ||
response, err := service.client.do(ctx, http.MethodGet, "/v1/discount-redemptions") | ||
if err != nil { | ||
return nil, response, err | ||
} | ||
|
||
redemptions := new(DiscountRedemptionsApiResponse) | ||
if err = json.Unmarshal(*response.Body, redemptions); err != nil { | ||
return nil, response, err | ||
} | ||
|
||
return redemptions, response, nil | ||
} |
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,95 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/NdoleStudio/lemonsqueezy-go/internal/helpers" | ||
"github.com/NdoleStudio/lemonsqueezy-go/internal/stubs" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDiscountRedemptionsService_Get(t *testing.T) { | ||
// Setup | ||
t.Parallel() | ||
|
||
// Arrange | ||
server := helpers.MakeTestServer(http.StatusOK, stubs.DiscountRedemptionGetResponse()) | ||
client := New(WithBaseURL(server.URL)) | ||
|
||
// Act | ||
redemption, response, err := client.DiscountRedemptions.Get(context.Background(), "1") | ||
|
||
// Assert | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) | ||
assert.Equal(t, stubs.DiscountRedemptionGetResponse(), *response.Body) | ||
assert.Equal(t, "1", redemption.Data.ID) | ||
|
||
// Teardown | ||
server.Close() | ||
} | ||
|
||
func TestDiscountRedemptionsService_GetWithError(t *testing.T) { | ||
// Setup | ||
t.Parallel() | ||
|
||
// Arrange | ||
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) | ||
client := New(WithBaseURL(server.URL)) | ||
|
||
// Act | ||
_, response, err := client.DiscountRedemptions.Get(context.Background(), "1") | ||
|
||
// Assert | ||
assert.NotNil(t, err) | ||
|
||
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) | ||
|
||
// Teardown | ||
server.Close() | ||
} | ||
|
||
func TestDiscountRedemptionsService_List(t *testing.T) { | ||
// Setup | ||
t.Parallel() | ||
|
||
// Arrange | ||
server := helpers.MakeTestServer(http.StatusOK, stubs.DiscountRedemptionsListResponse()) | ||
client := New(WithBaseURL(server.URL)) | ||
|
||
// Act | ||
redemptions, response, err := client.DiscountRedemptions.List(context.Background()) | ||
|
||
// Assert | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) | ||
assert.Equal(t, stubs.DiscountRedemptionsListResponse(), *response.Body) | ||
assert.Equal(t, 1, len(redemptions.Data)) | ||
|
||
// Teardown | ||
server.Close() | ||
} | ||
|
||
func TestDiscountRedemptionsService_ListWithError(t *testing.T) { | ||
// Setup | ||
t.Parallel() | ||
|
||
// Arrange | ||
server := helpers.MakeTestServer(http.StatusInternalServerError, nil) | ||
client := New(WithBaseURL(server.URL)) | ||
|
||
// Act | ||
_, response, err := client.DiscountRedemptions.List(context.Background()) | ||
|
||
// Assert | ||
assert.NotNil(t, err) | ||
|
||
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) | ||
|
||
// Teardown | ||
server.Close() | ||
} |
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,107 @@ | ||
package stubs | ||
|
||
// DiscountRedemptionGetResponse is a dummy response to the GET /v1/discount-redemption/:id endpoint | ||
func DiscountRedemptionGetResponse() []byte { | ||
return []byte(` | ||
{ | ||
"jsonapi": { | ||
"version": "1.0" | ||
}, | ||
"links": { | ||
"self": "https://api.lemonsqueezy.com/v1/discount-redemptions/1" | ||
}, | ||
"data": { | ||
"type": "discount-redemptions", | ||
"id": "1", | ||
"attributes": { | ||
"discount_id": 1, | ||
"order_id": 1, | ||
"discount_name": "10%", | ||
"discount_code": "10PERC", | ||
"discount_amount": 10, | ||
"discount_amount_type": "percent", | ||
"amount": 999, | ||
"created_at": "2023-02-07T10:30:01.000000Z", | ||
"updated_at": "2023-02-07T10:30:01.000000Z" | ||
}, | ||
"relationships": { | ||
"discount": { | ||
"links": { | ||
"related": "https://api.lemonsqueezy.com/v1/discount-redemptions/1/discount", | ||
"self": "https://api.lemonsqueezy.com/v1/discount-redemptions/1/relationships/discount" | ||
} | ||
}, | ||
"order": { | ||
"links": { | ||
"related": "https://api.lemonsqueezy.com/v1/discount-redemptions/1/order", | ||
"self": "https://api.lemonsqueezy.com/v1/discount-redemptions/1/relationships/order" | ||
} | ||
} | ||
}, | ||
"links": { | ||
"self": "https://api.lemonsqueezy.com/v1/discount-redemptions/1" | ||
} | ||
} | ||
} | ||
`) | ||
} | ||
|
||
// DiscountRedemptionsListResponse is a dummy response to GET /v1/discount-redemption | ||
func DiscountRedemptionsListResponse() []byte { | ||
return []byte(` | ||
{ | ||
"meta": { | ||
"page": { | ||
"currentPage": 1, | ||
"from": 1, | ||
"lastPage": 1, | ||
"perPage": 10, | ||
"to": 10, | ||
"total": 10 | ||
} | ||
}, | ||
"jsonapi": { | ||
"version": "1.0" | ||
}, | ||
"links": { | ||
"first": "https://api.lemonsqueezy.com/v1/discount-redemptions?page%5Bnumber%5D=1&page%5Bsize%5D=10&sort=-createdAt", | ||
"last": "https://api.lemonsqueezy.com/v1/discount-redemptions?page%5Bnumber%5D=1339&page%5Bsize%5D=10&sort=-createdAt", | ||
"next": "https://api.lemonsqueezy.com/v1/discount-redemptions?page%5Bnumber%5D=2&page%5Bsize%5D=10&sort=-createdAt" | ||
}, | ||
"data": [ | ||
{ | ||
"type": "discount-redemptions", | ||
"id": "1", | ||
"attributes": { | ||
"discount_id": 1, | ||
"order_id": 1, | ||
"discount_name": "10%", | ||
"discount_code": "10PERC", | ||
"discount_amount": 10, | ||
"discount_amount_type": "percent", | ||
"amount": 999, | ||
"created_at": "2023-02-07T10:30:01.000000Z", | ||
"updated_at": "2023-02-07T10:30:01.000000Z" | ||
}, | ||
"relationships": { | ||
"discount": { | ||
"links": { | ||
"related": "https://api.lemonsqueezy.com/v1/discount-redemptions/1/discount", | ||
"self": "https://api.lemonsqueezy.com/v1/discount-redemptions/1/relationships/discount" | ||
} | ||
}, | ||
"order": { | ||
"links": { | ||
"related": "https://api.lemonsqueezy.com/v1/discount-redemptions/1/order", | ||
"self": "https://api.lemonsqueezy.com/v1/discount-redemptions/1/relationships/order" | ||
} | ||
} | ||
}, | ||
"links": { | ||
"self": "https://api.lemonsqueezy.com/v1/discount-redemptions/1" | ||
} | ||
} | ||
] | ||
} | ||
`) | ||
} |
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