Page MenuHomeSoftware Heritage

Add support for content negotiation in swh's API
ClosedPublic

Authored by douardda on Jan 24 2019, 9:29 AM.

Details

Summary

Using these 2 negotiate formatters, it is possible to write Flask endpoints
as:

from flask import Flask
from negotiate.flask import negotiate
from swh.core.api import JSONFormatter, MshpackFormatter

app = Flask()

@app.route('/somewhere')
@negotiate(MsgpackFormatter)
@negotiate(JSONFormatter)
def somewhere():
    return {'some': 'content'}

See https://github.com/nickstenning/negotiate

Diff Detail

Repository
rDCORE Foundations and core functionalities
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

douardda created this revision.Jan 24 2019, 9:29 AM

The idea behind this diff is to be able to use/play with internal APIs (scheduler, incexers, etc.) using cli tools (curl, httpie etc.)
In which case, the msgpack format is a pain in the neck. With this, the APIs can default to use json as response's content type, but honor the Accept header so the remote servers used in the code can still use msgpack as serialization format.

ardumont added inline comments.
swh/core/api.py
32

That won't be enough for all of our internal apis (but i have the feeling you know that and want to go incrementally ;)
I think of the storage for example but there might be others as well (indexer?).

ardumont added inline comments.Jan 24 2019, 10:16 AM
swh/core/api.py
159

why did you add that part?
That confuses me, i'm checking ;)

douardda marked 2 inline comments as done.Jan 24 2019, 10:29 AM
douardda added inline comments.
swh/core/api.py
32

Yes,so far this is enough for the scheduler (diff not yet submitted), but indeed it will need more tweaking...

159

In the context of an API client (remote service), we want to make sure we ask the API server for msgpack encoded response. The content-type only declare we send msgpack encoded payload, not what we ask for in return.

ardumont accepted this revision.Jan 24 2019, 10:32 AM
ardumont added inline comments.
swh/core/api.py
159

yes, remote! I misread. I keep forgetting that this code is the client part ;)

In the end, this was missing instruction all along (independent from the negotiate library introduction here).

Thanks.

This revision is now accepted and ready to land.Jan 24 2019, 10:32 AM
This revision was automatically updated to reflect the committed changes.