---
title: "#295: Building an API client with Sourcery Key/Value Annotations  🔮"
url: https://littlebitesofcocoa.com/295-building-an-api-client-with-sourcery-key-value-annotations
author: Jake Marsh
published: 2017-02-10
topics: ["Libraries","Foundation"]
description: "In Bite #294 we learned about annotations in Sourcery. They're a great way to make extra metadata available on our types when accessing them in..."
---

# #295: Building an API client with Sourcery Key/Value Annotations  🔮

By Jake Marsh, February 10, 2017. HTML: https://littlebitesofcocoa.com/295-building-an-api-client-with-sourcery-key-value-annotations

In [Bite #294](https://littlebitesofcocoa.com/294) we learned about **annotations** in [Sourcery](https://github.com/krzysztofzablocki/Sourcery). They're a great way to make extra metadata available on our types when accessing them in [Sourcery](https://github.com/krzysztofzablocki/Sourcery)'s `.stencil` templates.

Today we'll take a look at one of the most powerful features of **annotations** in [Sourcery](https://github.com/krzysztofzablocki/Sourcery), <i>**Key/Value Annotations**</i>.

We'll explore these by building a fictional API client powered by a simple **Swift Enum**. Let's begin!

First, we'll need to create our **enum**. Then, we'll define some **cases** to represent each **HTTP endpoint** we want to be able to call.

For us, these are a classic set of <i>"get all"</i>, <i>"get one"</i>, <i>"create"</i>, <i>"change"</i>, and <i>"delete"</i>:

``` swift
enum SpaceshipsAPI {
    case ships
    case ship(shipID: Int)
    case createShip(shipID: Int, ship: Ship)
    case updateShip(shipID: Int, ship: Ship)
    case deleteShip(shipID: Int)
}
```

Long-time readers may recognize this **enum** technique from _Bites_ such as [#93](https://littlebitesofcocoa.com/93) or [#150](https://littlebitesofcocoa.com/150).

Here's where the magic begins though. 🎩

[Sourcery](https://github.com/krzysztofzablocki/Sourcery) _Key/Value Annotations_ are similar to regular **Annotations**. Instead of a simple tag though, they allow us to annotate types, enums, enum cases, etc with a set of names and a values. 

_Key/Value Annotations_ work everywhere that regular **Annotations** do. We can annotate types, enums, enum cases, and more.

They look like this:

``` swift
/// sourcery: key = value
/// sourcery: anotherKey = someOtherValue
```

They can also be listed in a single line:

``` swift
/// sourcery: key = value, anotherKey = someOtherValue
```

Values can contain some basic types:

``` swift
/// sourcery: maxSpeed = 1500, hasHyperdrive = true
/// sourcery: codename = "blacksaber"
```

Ok, back to our API client. Now that we know we can add simple metadata like this above things in our code, let's use this technique to describe each API endpoint.

``` swift
enum SpaceshipsAPI {
    /// sourcery: method = "GET", path = "/spaceships"
    /// sourcery: timeout = 5
    case ships
    ...
}
```

Nice! Normally, we might put things like `method` and `path` into essentially a big `switch` statement `switch`ing on `self`, and returning the correct `method` or `path`.

With [Sourcery](https://github.com/krzysztofzablocki/Sourcery) _Key/Value Annotations_ though, we can let [Sourcery](https://github.com/krzysztofzablocki/Sourcery) <i>generate</i> all of these statements for us.

Let's create a `SpaceshipsAPI+Properties.stencil` template that extends our `enum`, generating the **computed properties**:

``` swift
extension SpaceshipsAPI {
  public var path: String? {
    switch self {
      {% for c in type.SpaceshipsAPI.cases %}
      {% if c.annotations.path %}
      case .{{ c.name }}: return "{{c.annotations.path}}"
      {% endif %}
    {% endfor %}
    default: return nil
    }
  }
  
  // ... etc
}
```

Success! Now we can work at a much higher level. We can update our **annotations** and never need to manually edit tedious `switch` statements.

We'll [run sourcery](https://littlebitesofcocoa.com/292-metaprogramming-with-sourcery), which creates/updates `SpaceshipsAPI+Properties.generated.swift`, filling it with all those beautiful switches. 

Finally, we can use our new **properties** like this:

``` swift
  print(SpaceshipsAPI.ships.path)
```

The use cases can begin to boggle the mind a bit here.

We can now add <i>rich metadata</i> to just about anything in our **Swift** code. Then use it to [metaprogram](https://en.wikipedia.org/wiki/Metaprogramming) <i>new</i> **Swift** code that we can use in our **project**.

Neat!
