What makes for a good Elixir API client?

I like writing API clients for fun and to learn new languages. They’re also a good way to grow the ecosystem.

What’s the idiomatic way to write clients in Elixir, with regards to serialization, relationships, etc? Should I send back a Map or a Struct? Should I take the time to be able to chain stuff together like say:

book = Client.get("books/1")

book.characters()
# This would return a list of Character structs (or maps)

Or is it just better to be simple and return the raw HTTPoison body?

3 Likes

I would suggest Structs when you know what data fields you will receive , and maps (with string keys!) for unknown/varying data.

4 Likes

Why do you suggest string keys @Qqwy?

1 Like

Data which comes from outside your application, broadly speaking, can’t be trusted. Given that atom allocation can lead to memory exhaustion in a long-running Erlang system, using atoms for external data opens up your app for a potential denial of service (DoS) attack.

Cheers!

5 Likes

Perhaps we should stop writing REST API clients? An easier way would be to consume a Swagger API spec and generate the API client a la swagger-codegen. We could write a generator for that codebase or just write our own in Elixir.

Edit: There is this project swagger-elixir but it’s incomplete and abandoned. It has gotten as far as parsing JSON/YAML files. :icon_neutral:

2 Likes

I will start by saying that I haven’t looked at swagger at all, but any time people start talking about APIs and code generation, I start thinking of SOAP and wsdl.

And when I start thinking about wsdl, I start shuddering…

6 Likes

You can go even further and do what http://jhipster.github.io/ has done ;). Basically you model database model and based on this it generates backedn and fronted part.

But anyway swagger is very useful when you have a lot of micro services and what to know what API they provides.
In Java word it is easy to generate. You just adding annotations in code next to rest interface. The all swagger API is generated automatic for annotations during build.

In my opinion REST is outdated in future there must be some more tight integration fronted - backed, like https://www.firebase.com/

2 Likes

Just want to share that Swagger Codegen just released stable version v2.2.3 with 11 new generators while the Elixir API client generator has been included since the 2.2.2 release.

If you guys have feedback or need help on the Elixir API client generator, please open a ticket (issue) in the Github repo.

5 Likes

UPDATE: myself and other top contributors (40+) have decided to fork Swagger Codegen to maintain a community-driven version called “OpenAPI Generator” (openapi-generator.tech), which supports both OpenAPI spec v2 and v3.

For the reasons behind the fork, please refer to the Q&A (github .com/OpenAPITools/openapi-generator/blob/master/docs/qna.md)

We already released several stable versions (github .com/OpenAPITools/openapi-generator/releases). Please give it a try and let us know if you’ve any feedback. The migration guide (github .com/OpenAPITools/openapi-generator/blob/master/docs/migration-from-swagger-codegen.md) is a good starting point.

1 Like