# Creating an API Helper Library for SwiftNIO | raywenderlich.com

**URL:** <https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101>\
**Category:** 🎓 Courses & Articles\
**Created:** [November 5, 2019, 7:24am UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101 "2019-11-05T07:24:54Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrlotu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mrlotu/32/96947_2.png) [@mrlotu](https://forums.kodeco.com/u/mrlotu)\
**Post date:** [November 5, 2019, 7:24am UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/1 "2019-11-05T07:24:54Z")

</div>

In this SwiftNIO tutorial you’ll learn how to utilize the helper types from SwiftNIO to create an API library that accesses the Star Wars API.

* * *
This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/4623672-creating-an-api-helper-library-for-swiftnio](https://www.raywenderlich.com/4623672-creating-an-api-helper-library-for-swiftnio)

---

<div class="post-metadata">

**Author:** ![isuru](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/isuru/32/104343_2.png) [@isuru](https://forums.kodeco.com/u/isuru)\
**Post date:** [November 6, 2019, 10:53am UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/2 "2019-11-06T10:53:54Z")

</div>

In the [code snippet](https://i.imgur.com/6wjiahp.png) right above the ‘Extending the Models’ section, it’s missing the generic R.

---

<div class="post-metadata">

**Author:** ![rae](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rae/32/81653_2.png) [@rae](https://forums.kodeco.com/u/rae)\
**Post date:** [November 6, 2019, 8:27pm UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/3 "2019-11-06T20:27:11Z")

</div>

Why doesn’t the code make use of `keyDecodingStrategy` to handle snake case, instead of doing the tiresome `skinColor = "skin_color"`?

Also, what’s with the use of `_` as an instance var prefix? Why was it used? e.g. `_homeworld`.

---

<div class="post-metadata">

**Author:** ![mrlotu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mrlotu/32/96947_2.png) [@mrlotu](https://forums.kodeco.com/u/mrlotu)\
**Post date:** [November 8, 2019, 9:40pm UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/4 "2019-11-08T21:40:49Z")

</div>

Hey @isuru, thanks for mentioning this. I’ve updated the snippet in the article.

@rae, the reason there is no use of `keyDecodingStrategy` is because of the `_homeworld` like properties. Since they’re not following any of the `keyDecodingStrategy` conventions (neither camel nor snake case) we have to write custom `CodingKeys` for them, and thus also for our other properties.

As for why we use these underscored properties in the first place: The `_homeworld` like properties are used to hold the data we get from the API. In this case a URL pointing to an other resource. Next to those, we have a non underscored property that’s a computed property, like this:

```swift
public var homeworld: EventLoopFuture<Planet> {
  return client.get(URL(string: _homeworld))
}

```

This means that instead of exposing a URL to our users in the `homeworld` property, we expose an `EventLoopFuture<Planet>`, which is more useful and clean, saving the end user from having to then go and fetch the actual Planet object. The `_` itself does nothing here, it’s purely a convention picked up from Python where private properties are prefixed with `_`. I hope this answers your question, if not, feel free to let me know, happy to further explain :]

---

<div class="post-metadata">

**Author:** ![tokopedia](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tokopedia/32/81494_2.png) [@tokopedia](https://forums.kodeco.com/u/tokopedia)\
**Post date:** [November 16, 2019, 6:00pm UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/5 "2019-11-16T18:00:16Z")

</div>

actually there’s typo in second update on `get` function on tutorial

```auto
func get<R>(_ route: URL?) -> EventLoopFuture where R: Decodable & SwapiModel

```

EventLoopFuture should abstract R

```auto
func get<R>(_ route: URL?) -> EventLoopFuture<R> where R: Decodable & SwapiModel

```

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [January 11, 2020, 8:47pm UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/6 "2020-01-11T20:47:09Z")

</div>

@mrlotu Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![mrlotu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mrlotu/32/96947_2.png) [@mrlotu](https://forums.kodeco.com/u/mrlotu)\
**Post date:** [January 11, 2020, 8:53pm UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/7 "2020-01-11T20:53:42Z")

</div>

@tokopedia Thanks for mentioning this! The tutorial was updated a while ago, but I totally forgot to reply back here. Thanks for the ping @shogunkaramazov!

---

<div class="post-metadata">

**Author:** ![acca](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/acca/32/102075_2.png) [@acca](https://forums.kodeco.com/u/acca)\
**Post date:** [June 4, 2020, 11:34am UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/8 "2020-06-04T11:34:42Z")

</div>

Try to build with docker, after run it with docker, it prompt an error:

```
[[http_1 | Fatal error: Error raised at top level: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(The operation could not be completed))): file /home/buildnode/jenkins/workspace/oss-swift-5.0-package-linux-ubuntu-18_04/swift/stdlib/public/core/ErrorType.swift, line 200

```

What should I do? ![68801591321448_.pic_hd](https://files.chunter.kodeco.com/original/3X/a/c/ac43772537ecd383568f4a1fd8e2ab2038016457.jpeg)

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 8, 2020, 2:19am UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/9 "2020-07-08T02:19:59Z")

</div>

@acca Does this happen with the sample project too?

---

<div class="post-metadata">

**Author:** ![acca](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/acca/32/102075_2.png) [@acca](https://forums.kodeco.com/u/acca)\
**Post date:** [July 8, 2020, 7:49am UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/10 "2020-07-08T07:49:39Z")

</div>

Yes, it still not work!

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 8, 2020, 4:51pm UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/11 "2020-07-08T16:51:59Z")

</div>

@mrlotu Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![mrlotu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mrlotu/32/96947_2.png) [@mrlotu](https://forums.kodeco.com/u/mrlotu)\
**Post date:** [July 14, 2020, 6:59am UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/12 "2020-07-14T06:59:14Z")

</div>

Thanks for raising @acca & @shogunkaramazov. I did some digging and turns out [swapi.co](http://swapi.co) (the service used in the tutorial) has been shut down. A new version of the SWAPI lives at swapi.dev. I will look into updating the project files & tutorial. For now to work around this you can edit the following line `SwapiURLBuilder.swift`:

```swift
// Change
static let baseUrl = "https://swapi.co/api"
// Into
static let baseUrl = "https://swapi.dev/api"

```

Sorry for the confusion! :]

---

<div class="post-metadata">

**Author:** ![mrlotu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mrlotu/32/96947_2.png) [@mrlotu](https://forums.kodeco.com/u/mrlotu)\
**Post date:** [July 14, 2020, 7:09am UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/13 "2020-07-14T07:09:35Z")

</div>

The tutorial & project files have been updated! Thanks again for noticing :]

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 4, 2021, 11:48pm UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/14 "2021-04-04T23:48:34Z")

</div>

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 4, 2021, 11:48pm UTC](https://forums.kodeco.com/t/creating-an-api-helper-library-for-swiftnio-raywenderlich-com/87101/15 "2021-04-04T23:48:36Z")

</div>

Closed on April 4, 2021, 11:48pm UTC


