Passed
Push — main ( 6e8bbe...6c1cc7 )
by Acho
01:10
created

client.*VariantsService.Get   A

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 client
2
3
import (
4
	"context"
5
	"encoding/json"
6
	"net/http"
7
)
8
9
// VariantsService is the API client for the `/v1/variants` endpoint
10
type VariantsService service
11
12
// Get returns the variant with the given ID.
13
//
14
// https://docs.lemonsqueezy.com/api/variants#retrieve-a-variant
15
func (service *VariantsService) Get(ctx context.Context, variantID string) (*VariantApiResponse, *Response, error) {
16
	response, err := service.client.do(ctx, http.MethodGet, "/v1/variants/"+variantID)
17
	if err != nil {
18
		return nil, response, err
19
	}
20
21
	variant := new(VariantApiResponse)
22
	if err = json.Unmarshal(*response.Body, variant); err != nil {
23
		return nil, response, err
24
	}
25
26
	return variant, response, nil
27
}
28
29
// List returns a paginated list of variants.
30
//
31
// https://docs.lemonsqueezy.com/api/variants#list-all-variants
32
func (service *VariantsService) List(ctx context.Context) (*VariantsApiResponse, *Response, error) {
33
	response, err := service.client.do(ctx, http.MethodGet, "/v1/variants")
34
	if err != nil {
35
		return nil, response, err
36
	}
37
38
	variants := new(VariantsApiResponse)
39
	if err = json.Unmarshal(*response.Body, variants); err != nil {
40
		return nil, response, err
41
	}
42
43
	return variants, response, nil
44
}
45