-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimports.go
110 lines (95 loc) · 4 KB
/
imports.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package jwplatform
import (
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// ImportResource is the resource that is returned for all Import resource requests,
// with the exception of the Create action, which extends this struct with upload-related data.
type ImportResource struct {
V2ResourceResponse
Metadata ImportReadMetadata `json:"metadata"`
TotalItemsIngested int `json:"total_items_ingested"`
LastImport string `json:"last_import"`
}
// ImportReadMetadata describes the read structure of an Import resource metadata.
// This extends the base metadata, including an additional field, Password, which cannot be updated
// on a write call (update/create)
type ImportReadMetadata struct {
ImportMetadata
Password string `json:"password"`
}
// ImportMetadata describes the metadata for an Import resource
type ImportMetadata struct {
URL string `json:"url"`
HostOnImport bool `json:"host_on_import"`
Title string `json:"title,omitempty"`
State string `json:"state"`
Type string `json:"type"`
Username string `json:"username,omitempty"`
Tags []string `json:"tags"`
IngestMetadata IngestMetadata `json:"ingest_metadata"`
IngestTags []string `json:"ingest_tags"`
}
// ImportWriteRequest is the request structure required for Import create calls.
type ImportWriteRequest struct {
Metadata ImportMetadata `json:"metadata"`
}
// IngestMetadata describes which data will be captured in the import from the
// MRSS feed.
type IngestMetadata struct {
Captions bool `json:"captions"`
Categories bool `json:"categories"`
Credits bool `json:"credits"`
Description bool `json:"description"`
Keywords bool `json:"keywords"`
PublishDate bool `json:"publish_date"`
Tags bool `json:"tags"`
Thumbnails bool `json:"thumbnails"`
}
// ImportResourcesResponse is the response structure for Import list calls.
type ImportResourcesResponse struct {
V2ResourcesResponse
Imports []ImportResource `json:"imports"`
}
// ImportsClient for interacting with V2 Imports API.
type ImportsClient struct {
v2Client *V2Client
}
// Get a single Import resource by ID.
func (c *ImportsClient) Get(siteID, importID string) (*ImportResource, error) {
importResource := &ImportResource{}
path := fmt.Sprintf("/v2/sites/%s/imports/%s", siteID, importID)
err := c.v2Client.Request(http.MethodGet, path, importResource, nil, nil)
return importResource, err
}
// Create a Import resource.
func (c *ImportsClient) Create(siteID string, importMetadata *ImportMetadata) (*ImportResource, error) {
createRequestData := &ImportWriteRequest{Metadata: *importMetadata}
importResource := &ImportResource{}
path := fmt.Sprintf("/v2/sites/%s/imports", siteID)
err := c.v2Client.Request(http.MethodPost, path, importResource, createRequestData, nil)
return importResource, err
}
// List all Import resources associated with a given Site ID.
func (c *ImportsClient) List(siteID string, queryParams *QueryParams) (*ImportResourcesResponse, error) {
importResources := &ImportResourcesResponse{}
path := fmt.Sprintf("/v2/sites/%s/imports", siteID)
urlValues, _ := query.Values(queryParams)
err := c.v2Client.Request(http.MethodGet, path, importResources, nil, urlValues)
return importResources, err
}
// Update a Import resource by ID.
func (c *ImportsClient) Update(siteID, importID string, importMetadata *ImportMetadata) (*ImportResource, error) {
updateRequestData := &ImportWriteRequest{Metadata: *importMetadata}
importResource := &ImportResource{}
path := fmt.Sprintf("/v2/sites/%s/imports/%s", siteID, importID)
err := c.v2Client.Request(http.MethodPatch, path, importResource, updateRequestData, nil)
return importResource, err
}
// Delete a Import resource by ID.
func (c *ImportsClient) Delete(siteID, importID string) error {
path := fmt.Sprintf("/v2/sites/%s/imports/%s", siteID, importID)
err := c.v2Client.Request(http.MethodDelete, path, nil, nil, nil)
return err
}