forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirect.go
88 lines (75 loc) · 2.9 KB
/
redirect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package goshopify
import (
"context"
"fmt"
)
const redirectsBasePath = "redirects"
// RedirectService is an interface for interacting with the redirects
// endpoints of the Shopify API.
// See https://help.shopify.com/api/reference/online_store/redirect
type RedirectService interface {
List(context.Context, interface{}) ([]Redirect, error)
Count(context.Context, interface{}) (int, error)
Get(context.Context, uint64, interface{}) (*Redirect, error)
Create(context.Context, Redirect) (*Redirect, error)
Update(context.Context, Redirect) (*Redirect, error)
Delete(context.Context, uint64) error
}
// RedirectServiceOp handles communication with the redirect related methods of the
// Shopify API.
type RedirectServiceOp struct {
client *Client
}
// Redirect represents a Shopify redirect.
type Redirect struct {
Id uint64 `json:"id"`
Path string `json:"path"`
Target string `json:"target"`
}
// RedirectResource represents the result from the redirects/X.json endpoint
type RedirectResource struct {
Redirect *Redirect `json:"redirect"`
}
// RedirectsResource represents the result from the redirects.json endpoint
type RedirectsResource struct {
Redirects []Redirect `json:"redirects"`
}
// List redirects
func (s *RedirectServiceOp) List(ctx context.Context, options interface{}) ([]Redirect, error) {
path := fmt.Sprintf("%s.json", redirectsBasePath)
resource := new(RedirectsResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Redirects, err
}
// Count redirects
func (s *RedirectServiceOp) Count(ctx context.Context, options interface{}) (int, error) {
path := fmt.Sprintf("%s/count.json", redirectsBasePath)
return s.client.Count(ctx, path, options)
}
// Get individual redirect
func (s *RedirectServiceOp) Get(ctx context.Context, redirectId uint64, options interface{}) (*Redirect, error) {
path := fmt.Sprintf("%s/%d.json", redirectsBasePath, redirectId)
resource := new(RedirectResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Redirect, err
}
// Create a new redirect
func (s *RedirectServiceOp) Create(ctx context.Context, redirect Redirect) (*Redirect, error) {
path := fmt.Sprintf("%s.json", redirectsBasePath)
wrappedData := RedirectResource{Redirect: &redirect}
resource := new(RedirectResource)
err := s.client.Post(ctx, path, wrappedData, resource)
return resource.Redirect, err
}
// Update an existing redirect
func (s *RedirectServiceOp) Update(ctx context.Context, redirect Redirect) (*Redirect, error) {
path := fmt.Sprintf("%s/%d.json", redirectsBasePath, redirect.Id)
wrappedData := RedirectResource{Redirect: &redirect}
resource := new(RedirectResource)
err := s.client.Put(ctx, path, wrappedData, resource)
return resource.Redirect, err
}
// Delete an existing redirect.
func (s *RedirectServiceOp) Delete(ctx context.Context, redirectId uint64) error {
return s.client.Delete(ctx, fmt.Sprintf("%s/%d.json", redirectsBasePath, redirectId))
}