OpenFn / lightning

OpenFn/Lightning ⚡️ is the newest version of the OpenFn DPG and provides a web UI to visually manage complex workflow automation projects.
https://openfn.github.io/lightning/
GNU Lesser General Public License v3.0
129 stars 35 forks source link

Expose collections via an HTTP endpoint #2566

Open stuartc opened 2 weeks ago

stuartc commented 2 weeks ago

Re: https://github.com/OpenFn/lightning/issues/2190

Provide an HTTP endpoint that exposes methods that calls functions inside the Collection context.

Streaming is a requirement of this feature, we should be using features like send_chunked and chunk (from Plug).

Auth

Access to this API must be authenticated using either a Users access token, or a RunToken, in the Authentication Header

API spec

GET /collections

Fetches/searches for values within a collection

GET /collections/{collection_name}/{key}

Returns:

{
  results: [{ key, value, created, updated }],
  end_offset: 123
}

Each item is a { key , value, created, updated } object (I'd like a name for this wrapper - Record?)

Other metadata, like end_offset for paged results, is included at the top of the object as siblings of results

POST /collections

Upsert values into a collection

POST /collections/{collection_name}

Returns

For any 20x payload, return a JSON summary in the form:

{ upserted: 101, errors: [{}] }

DELETE /collections

Remove values from a collection

DELETE /collections/{collection_name}/{key}

Returns

If possible, return a JSON summary in the form:

{ deleted: 101, keys: [...]}
josephjclark commented 1 week ago

@stuartc

I guess we'll use use Authorization header like BEARER <token> for all requests? Probably base64 encoded. Run tokens aren't sensitive, but when we add client API keys they will be.

[EDIT: pending our conversation in slack maybe it's /collections/<collection_name>?key=xxx I think GET needs to be like this:

GET /collections/<collection_name>/<key-pattern> 

Where key-pattern is optional

With query parameters (now or in future): offset, limit, since/after?, until/before?

I think the POST is always an upsert (if if a record exists, it'll be removed)

I have an idea that the post body is an array of the form { key: string, record: any, meta: any }. Meta would allow us to do things like override timestamps (maybe), or set expiration (perhaps?) per record. I don't know if we need it but we could use that as a bucket of system stuff.

What, if anything, should post return? An array with a status for each item? Maybe something more like { upserted: 101, errors: [] }

We also need a DELETE /collections/<collection_name>/<key>. Should we support a key pattern here or is that too exciting?

josephjclark commented 1 week ago

Updated the main issue body for GET/POST/DELETE requests, as per the Canvas conversation