Skip to content

Commit

Permalink
refactor: consolidate http logic into client
Browse files Browse the repository at this point in the history
  • Loading branch information
SignalRichard authored May 3, 2023
2 parents 487b880 + f3b5e31 commit ca30e81
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 229 deletions.
84 changes: 21 additions & 63 deletions stackoverflow/client/answers.go
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
}
90 changes: 17 additions & 73 deletions stackoverflow/client/articles.go
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
}
Loading

0 comments on commit ca30e81

Please sign in to comment.