|
1 | 1 | package client |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
5 | 4 | "fmt" |
6 | | - "io" |
7 | 5 | "log" |
8 | | - "net/http" |
9 | 6 | "net/url" |
10 | | - "strconv" |
11 | 7 | "strings" |
12 | 8 | ) |
13 | 9 |
|
14 | 10 | func (c *Client) GetArticles(articleIDs *[]int, filter *string) (*[]Article, error) { |
15 | | - ids := make([]string, len(*articleIDs)) |
16 | | - for i, articleID := range *articleIDs { |
17 | | - ids[i] = strconv.Itoa(articleID) |
18 | | - } |
19 | | - log.Printf("%s/%s?team=%s&order=desc&sort=creation&filter=%s", "articles", strings.Join(ids, ";"), c.TeamName, *filter) |
20 | | - route := fmt.Sprintf("%s/%s?team=%s&order=desc&sort=creation&filter=%s", "articles", strings.Join(ids, ";"), c.TeamName, *filter) |
21 | | - req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", c.BaseURL, route), nil) |
22 | | - if err != nil { |
23 | | - return nil, err |
24 | | - } |
25 | | - |
26 | | - body, err := c.doRequest(req) |
| 11 | + response, err := c.get("articles", articleIDs, filter) |
27 | 12 | if err != nil { |
28 | 13 | return nil, err |
29 | 14 | } |
30 | 15 |
|
31 | | - responseWrapper := Wrapper[Article]{} |
32 | | - err = json.Unmarshal(body, &responseWrapper) |
| 16 | + articles, err := UnwrapResponseItems[Article](response) |
33 | 17 | if err != nil { |
34 | 18 | return nil, err |
35 | 19 | } |
36 | 20 |
|
37 | | - articles := responseWrapper.Items |
38 | | - |
39 | | - return &articles, nil |
| 21 | + return articles, nil |
40 | 22 | } |
41 | 23 |
|
42 | 24 | func (c *Client) CreateArticle(article *Article) (*Article, error) { |
43 | | - //rb, err := json.Marshal(&article) |
44 | | - //if err != nil { |
45 | | - // return nil, err |
46 | | - //} |
47 | | - //buf := new(strings.Builder) |
48 | | - //io.Copy(buf, strings.NewReader((string(rb)))) |
49 | | - //log.Printf("Deserialized JSON bytes: %s", buf.String()) |
50 | | - //req, err := http.NewRequest("POST", fmt.Sprintf("%s%s?team=%s", c.BaseURL, "articles/add", c.TeamName), bytes.NewReader(rb)) |
51 | | - |
52 | | - formData := GenerateArticleFormData(article) |
53 | | - req, err := http.NewRequest("POST", fmt.Sprintf("%s%s?team=%s", c.BaseURL, "articles/add", c.TeamName), strings.NewReader(formData)) |
54 | | - if err != nil { |
55 | | - return nil, err |
56 | | - } |
57 | | - |
58 | | - body, err := c.doRequest(req) |
| 25 | + response, err := c.create("articles/add", GenerateArticleFormData(article)) |
59 | 26 | if err != nil { |
60 | 27 | return nil, err |
61 | 28 | } |
62 | 29 |
|
63 | | - buf := new(strings.Builder) |
64 | | - io.Copy(buf, strings.NewReader((string(body)))) |
65 | | - log.Printf("Response body: %s", buf.String()) |
66 | | - |
67 | | - responseWrapper := Wrapper[Article]{} |
68 | | - err = json.Unmarshal(body, &responseWrapper) |
| 30 | + articles, err := UnwrapResponseItems[Article](response) |
69 | 31 | if err != nil { |
70 | 32 | return nil, err |
71 | 33 | } |
72 | 34 |
|
73 | | - if len(responseWrapper.Items) != 1 { |
74 | | - return nil, fmt.Errorf("response wrapper does not contain expected number of items (1); item length is %d", len(responseWrapper.Items)) |
| 35 | + if len(*articles) != 1 { |
| 36 | + return nil, fmt.Errorf("response wrapper does not contain expected number of items (1); item length is %d", len(*articles)) |
75 | 37 | } |
76 | 38 |
|
77 | | - newArticle := responseWrapper.Items[0] |
| 39 | + newArticle := (*articles)[0] |
78 | 40 |
|
79 | 41 | return &newArticle, nil |
80 | 42 | } |
81 | 43 |
|
82 | 44 | func (c *Client) UpdateArticle(article *Article) (*Article, error) { |
83 | | - //rb, err := json.Marshal(&article) |
84 | | - //if err != nil { |
85 | | - // return nil, err |
86 | | - //} |
87 | | - //req, err := http.NewRequest("POST", fmt.Sprintf("%s%s%s%s?team=%s", c.BaseURL, "articles/", strconv.Itoa(article.ID), "/edit", c.TeamName), strings.NewReader(string(rb))) |
88 | | - |
89 | | - formData := GenerateArticleFormData(article) |
90 | | - req, err := http.NewRequest("POST", fmt.Sprintf("%s%s%s%s?team=%s", c.BaseURL, "articles/", strconv.Itoa(article.ID), "/edit", c.TeamName), strings.NewReader(formData)) |
| 45 | + response, err := c.update(fmt.Sprintf("%s%d%s", "articles/", article.ID, "/edit"), GenerateArticleFormData(article)) |
91 | 46 | if err != nil { |
92 | 47 | return nil, err |
93 | 48 | } |
94 | 49 |
|
95 | | - body, err := c.doRequest(req) |
| 50 | + articles, err := UnwrapResponseItems[Article](response) |
96 | 51 | if err != nil { |
97 | 52 | return nil, err |
98 | 53 | } |
99 | 54 |
|
100 | | - responseWrapper := Wrapper[Article]{} |
101 | | - err = json.Unmarshal(body, &responseWrapper) |
102 | | - if err != nil { |
103 | | - return nil, err |
| 55 | + if len(*articles) != 1 { |
| 56 | + return nil, fmt.Errorf("response wrapper does not contain expected number of items (1); item length is %d", len(*articles)) |
104 | 57 | } |
105 | 58 |
|
106 | | - if len(responseWrapper.Items) != 1 { |
107 | | - return nil, fmt.Errorf("response wrapper does not contain expected number of items (1); item length is %d", len(responseWrapper.Items)) |
108 | | - } |
109 | | - |
110 | | - newArticle := responseWrapper.Items[0] |
| 59 | + newArticle := (*articles)[0] |
111 | 60 |
|
112 | 61 | return &newArticle, nil |
113 | 62 | } |
114 | 63 |
|
115 | | -func (c *Client) DeleteArticle(articleId int, filter *string) error { |
116 | | - req, err := http.NewRequest("POST", fmt.Sprintf("%s%s%s%s?team=%s&filter=%s", c.BaseURL, "articles/", strconv.Itoa(articleId), "/delete", c.TeamName, *filter), nil) |
| 64 | +func (c *Client) DeleteArticle(articleId int) error { |
| 65 | + err := c.delete(fmt.Sprintf("%s%d%s", "articles/", articleId, "/delete")) |
117 | 66 | if err != nil { |
118 | 67 | return err |
119 | 68 | } |
120 | 69 |
|
121 | | - _, err2 := c.doRequest(req) |
122 | | - if err2 != nil { |
123 | | - return err |
124 | | - } |
125 | | - |
126 | 70 | return nil |
127 | 71 | } |
128 | 72 |
|
129 | | -func GenerateArticleFormData(article *Article) string { |
| 73 | +func GenerateArticleFormData(article *Article) *string { |
130 | 74 | formData := fmt.Sprintf("title=%s&body=%s&tags=%s&article_type=%s&filter=%s", url.QueryEscape(article.Title), url.QueryEscape(article.BodyMarkdown), strings.Join(article.Tags, ","), article.ArticleType, article.Filter) |
131 | 75 | log.Printf("Form data: %s", formData) |
132 | | - return formData |
| 76 | + return &formData |
133 | 77 | } |
0 commit comments