ATLG Sidebar
Cleanup
Welcome back! We're staying on the "sidebar" again this week, with a couple of small tweaks to make the code a tad more readable. I've also updated the GitHub repo with the code from the last couple of posts. So everything should be up to date!
Refactor Go Go Go
We're going to skip over the full code walkthrough since only a few minute details have changed. @ladydascalie was nice enough to point out a couple of small bits that could improve. I thought I'd cover them real quick in a bid to etch them into my mind, and pass them on. The first comes from my two URL "format" functions. While they are "correct" do exactly what they should, they are a bit verbose.
// FormatPagedRequest returns *http.Request ready to do() to get one page
func (dtc DevtoClient) FormatPagedRequest(param, paramValue string) (r *http.Request, err error) {
URL := dtc.DevtoAPIURL + "articles/?" + param + "=" + paramValue
fmt.Printf("%v\n", URL)
r, err = http.NewRequest(http.MethodGet, URL, nil)
if err != nil {
return nil, err
}
return r, nil
}
// FormatArticleRequest returns http.Request ready to do() and get an article
func (dtc DevtoClient) FormatArticleRequest(i int32) (r *http.Request, err error) {
URL := fmt.Sprintf(dtc.DevtoAPIURL+"articles/%d", i)
r, err = http.NewRequest(http.MethodGet, URL, nil)
if err != nil {
return nil, err
}
return r, nil
}
See how we are returning *http.Request, error
from both functions? Noticed anything else? That's right http.NewRequest()
also returns *http.Request, error
! That means we can remove our error check and return the results of the new request instead.
// FormatPagedRequest returns *http.Request ready to do() to get one page
func (dtc DevtoClient) FormatPagedRequest(param, paramValue string) (*http.Request, error) {
URL := dtc.DevtoAPIURL + "articles/?" + param + "=" + paramValue
fmt.Printf("%v\n", URL)
return http.NewRequest(http.MethodGet, URL, nil)
}
// FormatArticleRequest returns http.Request ready to do() and get an article
func (dtc DevtoClient) FormatArticleRequest(i int32) (*http.Request, error) {
URL := fmt.Sprintf(dtc.DevtoAPIURL+"articles/%d", i)
return http.NewRequest(http.MethodGet, URL, nil)
}
A little bit of refactoring allows us to move our error check up one level and not check the same thing twice.
Look Ma No Err
The second little tweak is the result of a missed error check. Which I'm a little surprised wasn't caught in linting. But, I suppose that's what we get for not giving our code a thorough re-reading.
json.Unmarshal(body, &articles)
Ahh if err != nil
! Now we are writing some Go!
err = json.Unmarshal(body, &articles)
if err != nil {
panic(err)
}
Next time
As I said, short and sweet this week! I'm actually thinking about doing at least one more post with this code base. I'd like to clean up all the panic()
's and try to build some sort of retry system. We could use a struct to keep track of the article ID
, the last HTTP error code, and the number of retries attempted so far. If dtc.Client.Do(r)
results in an HTTP status code of anything other then a 200 we can add it to the struct to retry. We would then wait for the complete run, then look at the struct and range through that if needed. Simple. Right? I guess we'll see next week.
You can find the code for this and most of the other Attempting to Learn Go posts in the repo on GitHub.
shindakun / atlg
Source repo for the "Attempting to Learn Go" posts I've been putting up over on dev.to
Attempting to Learn Go
Here you can find the code I've been writing for my Attempting to Learn Go posts that I've been writing and posting over on Dev.to.
Post Index
Enjoy this post? |
---|
How about buying me a coffee? |
Top comments (0)