Passed
Push — main ( b6acc3...d081c0 )
by Acho
01:28
created

client.*CheckoutsService.Create   A

Complexity

Conditions 4

Size

Total Lines 51
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 33
nop 2
dl 0
loc 51
rs 9.0879
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package lemonsqueezy
2
3
import (
4
	"context"
5
	"encoding/json"
6
	"net/http"
7
	"time"
8
)
9
10
// CheckoutsService is the API client for the `/v1/checkouts` endpoint
11
type CheckoutsService service
12
13
// Create a custom checkout.
14
//
15
// https://docs.lemonsqueezy.com/api/checkouts#create-a-checkout
16
func (service *CheckoutsService) Create(ctx context.Context, params *CheckoutCreateParams) (*CheckoutApiResponse, *Response, error) {
17
	checkoutData := map[string]any{
18
		"custom": params.CustomData,
19
	}
20
	if params.DiscountCode != nil {
21
		checkoutData["discount_code"] = params.DiscountCode
22
	}
23
24
	payload := map[string]any{
25
		"data": map[string]any{
26
			"type": "checkouts",
27
			"attributes": map[string]any{
28
				"custom_price": params.CustomPrice,
29
				"product_options": map[string]any{
30
					"enabled_variants": params.EnabledVariants,
31
				},
32
				"checkout_options": map[string]any{
33
					"button_color": params.ButtonColor,
34
				},
35
				"checkout_data": checkoutData,
36
				"expires_at":    params.ExpiresAt.Format(time.RFC3339),
37
				"preview":       true,
38
			},
39
			"relationships": map[string]any{
40
				"store": map[string]any{
41
					"data": map[string]any{
42
						"id":   params.StoreID,
43
						"type": "stores",
44
					},
45
				},
46
				"variant": map[string]any{
47
					"data": map[string]any{
48
						"id":   params.VariantID,
49
						"type": "variants",
50
					},
51
				},
52
			},
53
		},
54
	}
55
56
	response, err := service.client.do(ctx, http.MethodPost, "/v1/checkouts", payload)
57
	if err != nil {
58
		return nil, response, err
59
	}
60
61
	checkout := new(CheckoutApiResponse)
62
	if err = json.Unmarshal(*response.Body, checkout); err != nil {
63
		return nil, response, err
64
	}
65
66
	return checkout, response, nil
67
}
68
69
// Get the checkout with the given ID.
70
//
71
// https://docs.lemonsqueezy.com/api/checkouts#retrieve-a-checkout
72
func (service *CheckoutsService) Get(ctx context.Context, checkoutID string) (*CheckoutApiResponse, *Response, error) {
73
	response, err := service.client.do(ctx, http.MethodGet, "/v1/checkouts/"+checkoutID)
74
	if err != nil {
75
		return nil, response, err
76
	}
77
78
	checkout := new(CheckoutApiResponse)
79
	if err = json.Unmarshal(*response.Body, checkout); err != nil {
80
		return nil, response, err
81
	}
82
83
	return checkout, response, nil
84
}
85
86
// List returns a paginated list of checkouts.
87
//
88
// https://docs.lemonsqueezy.com/api/checkouts#list-all-checkouts
89
func (service *CheckoutsService) List(ctx context.Context) (*CheckoutsApiResponse, *Response, error) {
90
	response, err := service.client.do(ctx, http.MethodGet, "/v1/checkouts")
91
	if err != nil {
92
		return nil, response, err
93
	}
94
95
	checkouts := new(CheckoutsApiResponse)
96
	if err = json.Unmarshal(*response.Body, checkouts); err != nil {
97
		return nil, response, err
98
	}
99
100
	return checkouts, response, nil
101
}
102