Interacting with the Dev.to Article API - Again Sort Of
Tags: go, refactoring, atlg
Published: 2019-03-16 16:11:30

---
title: Interacting with the Dev.to Article API - Again Sort Of
published: true
description: "A couple of small tweaks on our previous Dev.to API code."
tags: go, golang, refactoring, atlg
cover_image: https://dev.shindakun.net/img/attemptinggo_w.png
series: "ATLG Sidebar"
---

# ATLG Sidebar

## Cleanup

[Welcome back](https://dev.to/shindakun/interacting-with-the-devto-article-api-4g34)! 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](https://github.com/shindakun/atlg) 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.

```go
// 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.

```go
// 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.

```go
json.Unmarshal(body, &articles)
```

Ahh `if err != nil`! Now we are writing some Go!

```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.

{% github shindakun/atlg %}

***

| Enjoy this post? |
|:----:|
| [How about buying me a coffee?](https://ko-fi.com/shindakun) |

***