-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
162 lines (131 loc) · 3.52 KB
/
api.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"encoding/json"
"errors"
"fmt"
"path/filepath"
"strconv"
"strings"
"github.com/automattic/go/jaguar"
)
func getApiFetcher(endpoint string) (j jaguar.Jaguar) {
url := strings.Join([]string{conf.SiteURL, "wp-json", endpoint}, "/")
j = jaguar.New()
j.Header.Add("Authorization", "Bearer "+conf.Token)
j.Url(url)
return j
}
// create new post
func createPost(post Post) (Post, error) {
j := getApiFetcher("wp/v2/posts")
j.Params.Add("title", post.Title)
j.Params.Add("date", post.Date)
j.Params.Add("content", post.Content)
j.Params.Add("status", post.Status)
j.Params.Add("publicize", "0")
resp, err := j.Method("POST").Send()
if err != nil {
return post, err
}
if resp.StatusCode > 299 {
errMsg := fmt.Sprintf("API Error [%v]: %v", resp.StatusCode, string(resp.Bytes))
return post, errors.New(errMsg)
}
err = json.Unmarshal(resp.Bytes, &post)
return post, err
}
// update existing post
func updatePost(post Post) (Post, error) {
api := fmt.Sprintf("wp/v2/posts/%v", post.Id)
j := getApiFetcher(api)
j.Params.Add("title", post.Title)
j.Params.Add("date", post.Date)
j.Params.Add("content", post.Content)
j.Params.Add("status", post.Status)
j.Params.Add("publicize", "0")
resp, err := j.Method("POST").Send()
if err != nil {
return post, err
}
if resp.StatusCode > 299 {
errMsg := fmt.Sprintf("API Error [%v]: %v", resp.StatusCode, string(resp.Bytes))
return post, errors.New(errMsg)
}
err = json.Unmarshal(resp.Bytes, &post)
return post, err
}
// create new page
func createPage(page Page) (Page, error) {
j := getApiFetcher("wp/v2/pages")
j.Params.Add("title", page.Title)
j.Params.Add("content", page.Content)
j.Params.Add("status", page.Status)
if page.Template != "" {
j.Params.Add("template", page.Template)
}
if page.ParentId != 0 {
j.Params.Add("parent", strconv.Itoa(page.ParentId))
}
if page.Order != "" {
j.Params.Add("menu_order", page.Order)
}
resp, err := j.Method("POST").Send()
log.Debug("Making request", string(resp.Bytes))
if err != nil {
return page, err
}
if resp.StatusCode > 299 {
errMsg := fmt.Sprintf("API Error [%v]: %v", resp.StatusCode, string(resp.Bytes))
return page, errors.New(errMsg)
}
err = json.Unmarshal(resp.Bytes, &page)
return page, err
}
// update existing page
func updatePage(page Page) (Page, error) {
api := fmt.Sprintf("wp/v2/pages/%v", page.Id)
j := getApiFetcher(api)
j.Params.Add("title", page.Title)
j.Params.Add("content", page.Content)
j.Params.Add("status", page.Status)
if page.Template != "" {
j.Params.Add("template", page.Template)
}
if page.ParentId != 0 {
j.Params.Add("parent", strconv.Itoa(page.ParentId))
}
if page.Order != "" {
j.Params.Add("menu_order", page.Order)
}
resp, err := j.Method("POST").Send()
if err != nil {
return page, err
}
if resp.StatusCode > 299 {
errMsg := fmt.Sprintf("API Error [%v]: %v", resp.StatusCode, string(resp.Bytes))
return page, errors.New(errMsg)
}
err = json.Unmarshal(resp.Bytes, &page)
return page, err
}
// upload a single file
func uploadMedia(media Media) (m Media, err error) {
j := getApiFetcher("wp/v2/media")
j.Files["file"] = filepath.Join("media", media.LocalFile)
resp, err := j.Method("POST").Send()
if err != nil {
return m, err
}
if resp.StatusCode > 299 {
errMsg := fmt.Sprintf("API Error [%v]: %v", resp.StatusCode, string(resp.Bytes))
return m, errors.New(errMsg)
}
err = json.Unmarshal(resp.Bytes, &m)
if err != nil {
return m, err
}
media.URL = m.URL
media.Link = m.Link
media.Id = m.Id
return media, nil
}