-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: consolidate http logic into client
- Loading branch information
Showing
8 changed files
with
182 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,82 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func (c *Client) GetAnswers(answerIDs *[]int, filter *string) (*[]Answer, error) { | ||
ids := make([]string, len(*answerIDs)) | ||
for i, answerID := range *answerIDs { | ||
ids[i] = strconv.Itoa(answerID) | ||
} | ||
log.Printf("%s/%s?team=%s&order=desc&sort=creation&filter=%s", "answers", strings.Join(ids, ";"), c.TeamName, *filter) | ||
route := fmt.Sprintf("%s/%s?team=%s&order=desc&sort=creation&filter=%s", "answers", strings.Join(ids, ";"), c.TeamName, *filter) | ||
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", c.BaseURL, route), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.doRequest(req) | ||
response, err := c.get("answers", answerIDs, filter) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
responseWrapper := Wrapper[Answer]{} | ||
err = json.Unmarshal(body, &responseWrapper) | ||
answers, err := UnwrapResponseItems[Answer](response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
answers := responseWrapper.Items | ||
|
||
return &answers, nil | ||
return answers, nil | ||
} | ||
|
||
func (c *Client) CreateAnswer(answer *Answer) (*Answer, error) { | ||
formData := GenerateAnswerFormData(answer, true) | ||
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s%s%s?team=%s", c.BaseURL, "questions/", strconv.Itoa(answer.QuestionID), "/answers/add", c.TeamName), strings.NewReader(formData)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.doRequest(req) | ||
response, err := c.create(fmt.Sprintf("%s%d%s", "questions/", answer.QuestionID, "/answers/add"), GenerateAnswerFormData(answer, true)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
buf := new(strings.Builder) | ||
io.Copy(buf, strings.NewReader((string(body)))) | ||
log.Printf("Response body: %s", buf.String()) | ||
|
||
responseWrapper := Wrapper[Answer]{} | ||
err = json.Unmarshal(body, &responseWrapper) | ||
answers, err := UnwrapResponseItems[Answer](response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(responseWrapper.Items) != 1 { | ||
return nil, fmt.Errorf("response wrapper does not contain expected number of items (1); item length is %d", len(responseWrapper.Items)) | ||
if len(*answers) != 1 { | ||
return nil, fmt.Errorf("response wrapper does not contain expected number of items (1); item length is %d", len(*answers)) | ||
} | ||
|
||
newAnswer := responseWrapper.Items[0] | ||
newAnswer := (*answers)[0] | ||
|
||
return &newAnswer, nil | ||
} | ||
|
||
func (c *Client) UpdateAnswer(answer *Answer) (*Answer, error) { | ||
formData := GenerateAnswerFormData(answer, false) | ||
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s%s%s?team=%s", c.BaseURL, "answers/", strconv.Itoa(answer.ID), "/edit", c.TeamName), strings.NewReader(formData)) | ||
response, err := c.update(fmt.Sprintf("%s%d%s", "answers/", answer.ID, "/edit"), GenerateAnswerFormData(answer, false)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.doRequest(req) | ||
answers, err := UnwrapResponseItems[Answer](response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
responseWrapper := Wrapper[Answer]{} | ||
err = json.Unmarshal(body, &responseWrapper) | ||
if err != nil { | ||
return nil, err | ||
if len(*answers) != 1 { | ||
return nil, fmt.Errorf("response wrapper does not contain expected number of items (1); item length is %d", len(*answers)) | ||
} | ||
|
||
if len(responseWrapper.Items) != 1 { | ||
return nil, fmt.Errorf("response wrapper does not contain expected number of items (1); item length is %d", len(responseWrapper.Items)) | ||
} | ||
|
||
newAnswer := responseWrapper.Items[0] | ||
newAnswer := (*answers)[0] | ||
|
||
return &newAnswer, nil | ||
} | ||
|
||
func (c *Client) DeleteAnswer(answerId int, filter *string) error { | ||
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s%s%s?team=%s&filter=%s", c.BaseURL, "answers/", strconv.Itoa(answerId), "/delete", c.TeamName, *filter), nil) | ||
func (c *Client) DeleteAnswer(answerId int) error { | ||
err := c.delete(fmt.Sprintf("%s%d%s", "answers/", answerId, "/delete")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err2 := c.doRequest(req) | ||
if err2 != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func GenerateAnswerFormData(answer *Answer, isCreate bool) string { | ||
func GenerateAnswerFormData(answer *Answer, isCreate bool) *string { | ||
formData := "" | ||
if isCreate { | ||
formData := fmt.Sprintf("id=%d&body=%s&preview=%t&filter=%s", answer.QuestionID, url.QueryEscape(answer.BodyMarkdown), answer.Preview, answer.Filter) | ||
log.Printf("Form data: %s", formData) | ||
return formData | ||
formData = fmt.Sprintf("id=%d&body=%s&preview=%t&filter=%s", answer.QuestionID, url.QueryEscape(answer.BodyMarkdown), answer.Preview, answer.Filter) | ||
} else { | ||
formData = fmt.Sprintf("id=%d&body=%s&preview=%t&filter=%s", answer.ID, url.QueryEscape(answer.BodyMarkdown), answer.Preview, answer.Filter) | ||
} | ||
|
||
formData := fmt.Sprintf("id=%d&body=%s&preview=%t&filter=%s", answer.ID, url.QueryEscape(answer.BodyMarkdown), answer.Preview, answer.Filter) | ||
log.Printf("Form data: %s", formData) | ||
return formData | ||
return &formData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,77 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func (c *Client) GetArticles(articleIDs *[]int, filter *string) (*[]Article, error) { | ||
ids := make([]string, len(*articleIDs)) | ||
for i, articleID := range *articleIDs { | ||
ids[i] = strconv.Itoa(articleID) | ||
} | ||
log.Printf("%s/%s?team=%s&order=desc&sort=creation&filter=%s", "articles", strings.Join(ids, ";"), c.TeamName, *filter) | ||
route := fmt.Sprintf("%s/%s?team=%s&order=desc&sort=creation&filter=%s", "articles", strings.Join(ids, ";"), c.TeamName, *filter) | ||
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", c.BaseURL, route), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.doRequest(req) | ||
response, err := c.get("articles", articleIDs, filter) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
responseWrapper := Wrapper[Article]{} | ||
err = json.Unmarshal(body, &responseWrapper) | ||
articles, err := UnwrapResponseItems[Article](response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
articles := responseWrapper.Items | ||
|
||
return &articles, nil | ||
return articles, nil | ||
} | ||
|
||
func (c *Client) CreateArticle(article *Article) (*Article, error) { | ||
//rb, err := json.Marshal(&article) | ||
//if err != nil { | ||
// return nil, err | ||
//} | ||
//buf := new(strings.Builder) | ||
//io.Copy(buf, strings.NewReader((string(rb)))) | ||
//log.Printf("Deserialized JSON bytes: %s", buf.String()) | ||
//req, err := http.NewRequest("POST", fmt.Sprintf("%s%s?team=%s", c.BaseURL, "articles/add", c.TeamName), bytes.NewReader(rb)) | ||
|
||
formData := GenerateArticleFormData(article) | ||
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s?team=%s", c.BaseURL, "articles/add", c.TeamName), strings.NewReader(formData)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.doRequest(req) | ||
response, err := c.create("articles/add", GenerateArticleFormData(article)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
buf := new(strings.Builder) | ||
io.Copy(buf, strings.NewReader((string(body)))) | ||
log.Printf("Response body: %s", buf.String()) | ||
|
||
responseWrapper := Wrapper[Article]{} | ||
err = json.Unmarshal(body, &responseWrapper) | ||
articles, err := UnwrapResponseItems[Article](response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(responseWrapper.Items) != 1 { | ||
return nil, fmt.Errorf("response wrapper does not contain expected number of items (1); item length is %d", len(responseWrapper.Items)) | ||
if len(*articles) != 1 { | ||
return nil, fmt.Errorf("response wrapper does not contain expected number of items (1); item length is %d", len(*articles)) | ||
} | ||
|
||
newArticle := responseWrapper.Items[0] | ||
newArticle := (*articles)[0] | ||
|
||
return &newArticle, nil | ||
} | ||
|
||
func (c *Client) UpdateArticle(article *Article) (*Article, error) { | ||
//rb, err := json.Marshal(&article) | ||
//if err != nil { | ||
// return nil, err | ||
//} | ||
//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))) | ||
|
||
formData := GenerateArticleFormData(article) | ||
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)) | ||
response, err := c.update(fmt.Sprintf("%s%d%s", "articles/", article.ID, "/edit"), GenerateArticleFormData(article)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.doRequest(req) | ||
articles, err := UnwrapResponseItems[Article](response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
responseWrapper := Wrapper[Article]{} | ||
err = json.Unmarshal(body, &responseWrapper) | ||
if err != nil { | ||
return nil, err | ||
if len(*articles) != 1 { | ||
return nil, fmt.Errorf("response wrapper does not contain expected number of items (1); item length is %d", len(*articles)) | ||
} | ||
|
||
if len(responseWrapper.Items) != 1 { | ||
return nil, fmt.Errorf("response wrapper does not contain expected number of items (1); item length is %d", len(responseWrapper.Items)) | ||
} | ||
|
||
newArticle := responseWrapper.Items[0] | ||
newArticle := (*articles)[0] | ||
|
||
return &newArticle, nil | ||
} | ||
|
||
func (c *Client) DeleteArticle(articleId int, filter *string) error { | ||
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) | ||
func (c *Client) DeleteArticle(articleId int) error { | ||
err := c.delete(fmt.Sprintf("%s%d%s", "articles/", articleId, "/delete")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err2 := c.doRequest(req) | ||
if err2 != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func GenerateArticleFormData(article *Article) string { | ||
func GenerateArticleFormData(article *Article) *string { | ||
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) | ||
log.Printf("Form data: %s", formData) | ||
return formData | ||
return &formData | ||
} |
Oops, something went wrong.