New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Introduce JSON:API resource format for HAPIv2 #4876
Comments
I guess my own reaction is - this looks really nice for avoiding bikeshedding; but I don't think its featureset really overlaps with the problems we have specifically in the robot HTTP API, and I'm not sure whether the added complexity and dependencies would be worth it. |
@sfoster1 this is my initial thinking as well. In fact the exercise of writing this RFC swayed me a bit more towards that camp. The libraries that could potentially work with With that in mind, I have 2 follow up questions for folks:
then essentially it's a matter of doing this:
|
Thanks for this writeup @iansolano! This has been helpful for me to inspect my assumptions about the new HTTP API. tl;drI see value in JSON:API, but I also think it's not quite right for our robot API because:
I think we should:
During development of HAPIv2:
Likes
Dislikes
Neutral
|
@mcous thanks! Yeah, I think I'm pretty much in exact agreement with your assessment. |
Same. |
Regarding FastAPI compatibility, i'd check https://pypi.org/project/pydantic-jsonapi/ out. FastAPI's data modeling/validation/schema generation is entirely built upon Pydantic. |
I'm in agreement with you all. This looks quite complex to me. All the relationships and linked items seem like overkill for our uses. |
Not moving forward with JSON:API, but we are taking lots of inspiration for it for our custom consistent response schema |
Ref: #4858
What is JSON:API
JSON API is a format that works with HTTP. It delineates how clients should request or edit data from a server, and how the server should respond to said requests. A main goal of the specification is to optimize HTTP requests; both in terms of the number of requests and the size of data packages exchanged between clients and servers.
If you’ve ever argued with your team about the way your JSON responses should be formatted, JSON:API can be your anti-bikeshedding tool.
By following shared conventions, you can increase productivity, take advantage of generalized tooling, and focus on what matters: your application.
Clients built around JSON:API are able to take advantage of its features around efficiently caching responses, sometimes eliminating network requests entirely.
Here’s an example response from a blog that implements JSON:API:
The response above contains the first in a collection of “articles”, as well as links to subsequent members in that collection. It also contains resources linked to the article, including its author and comments. Last but not least, links are provided that can be used to fetch or update any of these resources.
JSON:API covers creating and updating resources as well, not just responses.
Benefits From Using JSON API
Now that we have a basic idea of what JSON API is, what are some unique benefits that make it stand out?
Compound Documents
Compound documents is a unique ability in JSON API, allowing servers to send related resources alongside the requested primary resources — if implemented correctly this could decrease the number of necessary HTTP requests. Compound documents works by using the include parameter like as follows:
GET https://api.example.com/posts?include=author
This enables you to include additional resources in an initial request.
Sparse Fieldsets
If you’re using compound documents to include related resources, you could run into an issue of having large responses. Once again, JSON API has a solution.
Another unique aspect of JSON API are sparse fieldsets, which enable clients to only request data from specific fields. It works by adding the field you want to retrieve to the URI parameter with the resource name and the fields you want. This offers additional customization and can decrease bloat. It looks something like:
Sparse fieldsets is a standardized method of allowing clients to specify only the properties they want from an object to be included in the response. Using sparse fieldsets, you get only the fields that you desire, offering unique customization potential that is alluring for lean data sharing environments.
Optionality
Many of the features in JSONAPI.org are purely optional; you can turn them off or on. The features give the clients the power to determine what resources to accept. Getting clients to agree on how to retrieve and handle data is helpful as it erases redundancy and optimization to reduce bloat.
Optimization Features
JSON API comes equipped with many features to optimize the API return package. Special server side operations in JSON API include
sorting
, as well aspagination
; the ability to limit the number of returned resources to a subset, withfirst
,last
,next
, andprev
links. As well as, standardizing errors response patterns.Caching
“Because data changes affect fewer resources, there are fewer resources invalidated when data changes”
In JSON API use cases, caching is in essence built into HTTP. Since clients using JSON API access data in the same way, they don’t need to store data in various locations. This design may require a shift in thought, yet if used correctly can bring significant optimization benefits. For example: we can use
ETag
tokens in requests to match against the token and detect whether the resource has changed.Normalizing Responses in Clients
Having a standard response format, in this case JSON:API spec format means we can normalize our data in our clients (specifically, our Redux stores)
Normalizing JSON:API data:
https://github.com/yury-dymov/json-api-normalizer
Libraries like this do a number of things but one of the main benefits is it turns JSON:API responses from collections into maps - indexing resources by their
ids
which allow for constant time lookups.Considerations When Using JSON API
REST vs RPC
It is simply a fact that not everything in programming fits nicely into set definitions. There will be times where an endpoint should not be / can not be "RESTful", this will require us to break from JSON:API conventions. This is especially true for things like to batch (multiple) resource actions.
Verbosity
JSON:API is intentionally verbose. This isn't a huge deal especially considering we won't have a ton of relationships etc. and is also something we can fine tune excluding things like
links
andincludes
Applicability for Opentrons
This is where things are slightly fuzzier. If we were building a robust web/mobile application(s) I would say this is a much easier choice. Something like JSON:API spec or even better GraphQL would make a ton of sense. Although, this is not the case - some of these api endpoints and subsequent robot interactions don't necessarily fit nicely into this resource based model provided by JSON:API spec. However, the questions to answer is what we expect the future to hold, could this model makes sense for us currently, and how well does this convention support change within our system.
Libraries and FastAPI
My last concern is around FastAPI and choosing an implementation for JSON:API spec in python. There claim to be a couple framework agnostic libraries that do this:
Unfortunately, there are no implementations that mention being directly compatible with FastAPI. In the past, I've used JSON:API spec in tandem with a MVC framework which provided a lot of niceties that may be missing from these libraries.
The text was updated successfully, but these errors were encountered: