lemonsqueezy.*VariantsService.Get   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
package lemonsqueezy
2
3
import (
4
	"context"
5
	"encoding/json"
6
	"net/http"
7
	"strconv"
8
)
9
10
// VariantsService is the API client for the `/v1/variants` endpoint
11
type VariantsService service
12
13
// Get returns the variant with the given ID.
14
//
15
// https://docs.lemonsqueezy.com/api/variants#retrieve-a-variant
16
func (service *VariantsService) Get(ctx context.Context, variantID int) (*VariantAPIResponse, *Response, error) {
17
	response, err := service.client.do(ctx, http.MethodGet, "/v1/variants/"+strconv.Itoa(variantID))
18
	if err != nil {
19
		return nil, response, err
20
	}
21
22
	variant := new(VariantAPIResponse)
23
	if err = json.Unmarshal(*response.Body, variant); err != nil {
24
		return nil, response, err
25
	}
26
27
	return variant, response, nil
28
}
29
30
// List returns a paginated list of variants.
31
//
32
// https://docs.lemonsqueezy.com/api/variants#list-all-variants
33
func (service *VariantsService) List(ctx context.Context) (*VariantsAPIResponse, *Response, error) {
34
	response, err := service.client.do(ctx, http.MethodGet, "/v1/variants")
35
	if err != nil {
36
		return nil, response, err
37
	}
38
39
	variants := new(VariantsAPIResponse)
40
	if err = json.Unmarshal(*response.Body, variants); err != nil {
41
		return nil, response, err
42
	}
43
44
	return variants, response, nil
45
}
46