Perhaps this could be better asked as a task:
You’re designing a Json/REST API for an intermediate student of Elm.
What would it look like? What would you include? What wouldn’t you?
What materials would you use to teach it?
TL;DR: I need to understand API design on a higher level, but don’t want to go too far into the weeds, as I won’t be the one building out the architecture eventually. A lot of amateur/intermediate developers could use a solid guide to get started with Elm and json, heavily leaning towards simple documentation/guides.
API specs like JSON:API are complicated and overkill for this students needs, so would be great to get feedback on where to look for lighter, changeable, APIs. Understand this might be a bit too wide-ranging as questions go, but for students at this level it’s hard to know where to start.
Case Study: A Bad API?
I’ve been experimenting with the Open Library API which you can see in action here on Ellie App. I have to say, I don’t like it. I’m new to API design and doing some skim-reading about it, but their API seems inconsistent, messy, and not particularly pleasurable to work with:
-
Search and Work/Editions APIs seem to have no order:
- IDs are given rather than
Book
objects - IDs are in any order, in any language as far as I can tell, which makes the search not very useful for English readers
- IDs are given rather than
-
Chained
Http.get
requests are needed for fields like"authors"
(line 160
)- The thread link above looks useful, but I only half understand it
- It seems a lot of effort to build up a proper
Book
type in this way.
-
“Orphaned” structures like
"authors": [{"key": "/authors/OL34184A"}]
- Nearly all the books I’ve searched for only have this single object inside a list. It seems a pointless complication?
- Or it’s premature optimisation or forward planning?
-
Lists like
"isbn_13": ["9780140328721"]
where it’s mostly a single entry (why not just use aString
? -
Having to build URLs for
"covers": [8739161]
with the covers api, rather than supplying a full url I can pull in. -
Having to use
Json.Decode.maybe
A LOT instead ofJson.Decode.nullable
, because they’re missing (and notnull
) -
The API redirects book ISBNs to their book IDs (luckily Elm allows for redirects).
You can see my full comments (and gripes) in the Ellie App comments.
I’m all for simplicity
Caveats that are important to me, personally:
- I’m not a serious programmer and will hire eventually.
- I prefer simplicity wherever possible, avoiding complexity in the API.
- It’s likely to be mostly static, without a backend, using JsonBin or Postman.
POST
,GET
, andPUT
are needed, and data normalisation may be required.- It will be changeable (possibly often), but shouldn’t forward-plan too much.
- By the end you should be able to explain high-level concepts to another colleague or employee.
The API
- Which books would you suggest reading (ideally quickly)?
- Which videos are good to watch?
- I’ve heard good things about this Google API talk
- How do you spot a BAD Api?
- Is Open Library OK and I’m wrong?
- To be fair, books are quite difficult to model, especially library software
Elm Lang Specific
- Should we prefer a flatter API? as Elm prefers it’s
Model
? - Should we prefer simpler data structures?
- i.e: avoiding cases like
"authors"
above? - It’s possible to turn complicated structures to simpler ones, with
Json.Decode.map
, but it adds overhead.
- i.e: avoiding cases like
- What are the Gotchas that come up again and again?
Some Answers
- @m0n01d makes a good point about changing the Api shape for your
Model
- @lydell on why
Json.Decode.maybe
should be used with caution, and a wider discussion about dealing with optional fields.