Skip to content

Commit

Permalink
mataroa: refactor URL build in a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Aug 5, 2024
1 parent 1b7552b commit 6154685
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions mataroa.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,20 @@ type mataroaPatchRequest struct {
PublishedAt string `json:"published_at"`
}

func mustMataroaReq(method string, elem []string, body []byte) (p mataroaResponse, r *http.Response) {
func mustMataroaUrl(elem ...string) string {
// generate a Mataroa URL, ensure '/' at the end
reqUrl := must1(url.JoinPath(mataroaApiUrl, elem...))
reqUrl = must1(url.JoinPath(reqUrl, "/"))
mUrl := must1(url.JoinPath(mataroaApiUrl, elem...))
mUrl = must1(url.JoinPath(mUrl, "/"))
return mUrl
}

func mustMataroaReq(method string, url string, body []byte) (p mataroaResponse, r *http.Response) {
// Prepare request payload if non-nil
var reqBuf io.Reader
if body != nil {
reqBuf = bytes.NewBuffer(body)
}
req := must1(http.NewRequest(method, reqUrl, reqBuf))
req := must1(http.NewRequest(method, url, reqBuf))
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", mataroaToken))

Expand All @@ -82,7 +85,7 @@ func mustMataroaReq(method string, elem []string, body []byte) (p mataroaRespons
}

func mustGetMataroaPost(post post) (p mataroaResponse, r *http.Response) {
return mustMataroaReq("GET", []string{"posts", post.slug}, nil)
return mustMataroaReq("GET", mustMataroaUrl("posts", post.slug), nil)
}

func mustPatchMataroaPost(md goldmark.Markdown, post post) (p mataroaResponse, r *http.Response) {
Expand All @@ -94,7 +97,7 @@ func mustPatchMataroaPost(md goldmark.Markdown, post post) (p mataroaResponse, r
Slug: post.slug,
PublishedAt: post.date.Format(time.DateOnly),
}))
return mustMataroaReq("PATCH", []string{"posts", post.slug}, reqBody)
return mustMataroaReq("PATCH", mustMataroaUrl("posts", post.slug), reqBody)
}

func mustPostMataroaPost(md goldmark.Markdown, post post) (p mataroaResponse, r *http.Response) {
Expand All @@ -105,7 +108,7 @@ func mustPostMataroaPost(md goldmark.Markdown, post post) (p mataroaResponse, r
Body: buf.String(),
PublishedAt: post.date.Format(time.DateOnly),
}))
return mustMataroaReq("POST", []string{"posts"}, reqBody)
return mustMataroaReq("POST", mustMataroaUrl("posts"), reqBody)
}

func publishToMataroa(posts posts) {
Expand Down

0 comments on commit 6154685

Please sign in to comment.