Sapio Operations API

Integration Hub sends a combined labware reception and work order request from Sapio to one endpoint. Sequencescape checks the request, records it as an operation and answers straight away. The reception and the work order are processed afterwards, in the background.

Endpoint

POST /api/v2/sapio/operations

Header Value
Content-Type application/json
X-Sequencescape-Client-Id The Integration Hub API key

The body is plain JSON, not JSON:API. The endpoint is behind the y26_282_sapio_operations_endpoint feature flag; while the flag is off, it returns 404 FEATURE_DISABLED.

Request body

The body must match the work order schema, the contract between Integration Hub and Sequencescape. Its versions are kept in this repository, in config/schemas/work-order-schema/.

Sequencescape also needs request.external_id, a non-empty string: the request ID, which must be unique for the calling system.

Each environment checks bodies against one schema version, set in its configuration. The body's request.schema_version isn't used to choose it; a schema error's meta shows the version used.

The next version can be checked against while it is a work in progress (WIP), before it is agreed, in development, test and UAT environments. A schema error's meta then also says "wip": true: a WIP isn't agreed, and can still change. PSD tells Integration Hub when an environment it uses checks against a WIP.

The smallest valid body for version 1 of the schema: one tube with one sample, and a work order for the tube.

{
  "request": {
    "external_id": "request-1",
    "request_type": "reception_and_work_order",
    "schema_version": "1"
  },
  "labware": [
    {
      "uuid": "6f1c1d2e-0000-4000-8000-000000000001",
      "barcode": "TUBE-1",
      "type": "tube",
      "receptacles": [
        {
          "position": null,
          "uuid": "6f1c1d2e-0000-4000-8000-000000000002",
          "samples": [
            {
              "uuid": "6f1c1d2e-0000-4000-8000-000000000003",
              "study_uuid": "6f1c1d2e-0000-4000-8000-000000000004",
              "sanger_sample_id": "SAMPLE-1",
              "supplier_sample_name": "sample 1"
            }
          ]
        }
      ]
    }
  ],
  "work_order": {
    "external_id": "work-order-1",
    "service_name": "service",
    "requested_by": "user",
    "cost_code": "S0001",
    "requested_labware_identifiers": ["TUBE-1"],
    "options": {}
  }
}

Responses

Status Code When
202 A new request is accepted
200 The same request ID is sent again with the same payload
400 INVALID_JSON The body is not valid JSON
401 401 No API key, or an unknown one
403 FORBIDDEN An API key other than Integration Hub's
404 FEATURE_DISABLED The endpoint is switched off
409 PAYLOAD_CONFLICT The request ID was already used with a different payload
422 INVALID_STRUCTURE The body is not a JSON object, or request.external_id is missing
422 SCHEMA_VALIDATION_FAILED A new request doesn't match the schema
500 SCHEMA_UNAVAILABLE The schema can't be loaded; this is a Sequencescape configuration problem
503 SCHEMA_UNAVAILABLE The schema registry can't be reached; try again after Retry-After

Accepted and repeated requests

A new request gets 202; sending it again gets 200 with the same body. The status starts as queued.

{
  "operation_id": "c09a37c4-ce64-11f1-b688-1ae941b82136",
  "status": "queued"
}

Errors

Errors follow the JSON:API error format. Each has a status, code, title and detail, and a source.pointer (a JSON Pointer into the request body) when the error is about one part of it.

{
  "errors": [
    {
      "title": "Conflict",
      "detail": "The request ID request-1 was already used with a different payload.",
      "code": "PAYLOAD_CONFLICT",
      "source": { "pointer": "/request/external_id" },
      "status": "409"
    }
  ]
}

Schema errors

A new request that doesn't match the schema gets one error for each problem, with the schema and version it was checked against. At most the first 20 errors are returned; the top-level meta gives the total and how many are shown. For a missing property, the pointer is the object that lacks it, and the detail names the property.

{
  "errors": [
    {
      "title": "Payload Does Not Match Schema",
      "detail": "value at `/labware/0/type` is not one of: [\"plate96\", \"plate384\", \"tube\"]",
      "code": "SCHEMA_VALIDATION_FAILED",
      "source": { "pointer": "/labware/0/type" },
      "status": "422",
      "meta": { "schema": "work-order-schema", "version": 1 }
    },
    {
      "title": "Payload Does Not Match Schema",
      "detail": "object at `/labware/0` is missing required properties: barcode",
      "code": "SCHEMA_VALIDATION_FAILED",
      "source": { "pointer": "/labware/0" },
      "status": "422",
      "meta": { "schema": "work-order-schema", "version": 1 }
    }
  ],
  "meta": { "errors_total": 2, "errors_shown": 2 }
}

Checked against a WIP, each error's meta also has "wip": true, for example { "schema": "work-order-schema", "version": 2, "wip": true }.

Schema unavailable

A new request gets 500 SCHEMA_UNAVAILABLE when Sequencescape can't load the schema because of its own configuration, and 503 SCHEMA_UNAVAILABLE when the schema registry can't be reached. A 503 has a Retry-After header with the seconds to wait (60) before sending the request again with the same request ID. In both cases nothing is stored, and PSD is told the cause.

Repeated requests

  • A request ID is unique for each calling system. Sending the same request ID with the same payload returns the existing operation (200), and doesn't create or restart anything.
  • The same request ID with a different payload is a conflict (409).
  • Two requests with the same request ID that arrive together create one operation; the other is answered as a repeat.
  • Only new requests are checked against the schema, so a repeat always gets its operation back, whatever the schema version in use.
  • A rejected request (400, 422, 500 or 503) isn't stored, so its request ID stays unused and a corrected payload can be sent with it.

What counts as the same payload

Two payloads are the same when they hold the same JSON data:

  • Key order, whitespace and how a decimal is written (1.10 or 1.1) don't matter.
  • Array order, values, and whether a number is an integer or a decimal (1 or 1.0) do.

For Sequencescape developers

  • Feature flag: y26_282_sapio_operations_endpoint.

  • Settings, in config/environments/<env>.rb:

    Setting Development default (environment variable)
    config.sapio_schema_source local (SAPIO_SCHEMA_SOURCE)
    config.sapio_schema_subject work-order-schema (SAPIO_SCHEMA_SUBJECT)
    config.sapio_schema_version latest (SAPIO_SCHEMA_VERSION)
    config.sapio_schema_registry_url only for the redpanda source (SAPIO_SCHEMA_REGISTRY_URL)

    The source is local (files in this repository) or redpanda (a schema registry); development.rb has the registry settings commented out. The test environment has fixed values. Deployed environments get theirs from the deployment project (environment_template.rb.j2), and need them before the flag is switched on. Production uses a fixed version, never latest, so a new schema version only takes effect when it is deployed on purpose.

  • Schema files: config/schemas/<subject>/<version>.<extension>, for example config/schemas/work-order-schema/1.json. The extension gives the type (.json for JSON Schema, .avsc for Avro). Versions are whole numbers, and latest is the highest. As in a schema registry, all versions of a subject have the same type.

  • WIP schema files: the next version, while it is worked on, as record loaders' .wip.yml files: <version>.wip.<extension>, for example config/schemas/work-order-schema/2.wip.json. The local source reads WIP files only where config.deploy_wip_pipelines is on (development and test; deployed environments get it from the deployment project, which turns it on for UAT only). UAT reads them when its sapio_schema_source is local, with sapio_schema_version set to the WIP's number (for example 2): the same setting then reads 2.json once the WIP is renamed, and a newer WIP is used only when the setting is changed for it. There, a WIP counts as its version, including for latest, and a schema error's meta says "wip": true. Elsewhere it is ignored. A version with both a file and a WIP file (2.json and 2.wip.json) is an error (Schemas::LocalSource::DuplicateVersion). A schema registry has no WIP versions.

  • Changing the schema: a numbered version is never changed once it is released. Changes go in the next version's WIP file: a copy of the latest version with the changes, one WIP for each subject, shared by every story that changes the schema. Tests use latest, so they check the WIP; a change that the test payloads don't match fails the specs, as a backward compatible change shouldn't. When Integration Hub agree the WIP:

    1. Rename it, for example 2.wip.json to 2.json (unwip).
    2. Register the version in UAT's schema registry, so the registry holds only agreed versions.
    3. Set sapio_schema_version for each environment in the deployment project.
    4. Tell Integration Hub when each environment uses it.
  • Schema registry: the redpanda source reads <registry URL>/subjects/<subject>/versions/<version> through HTTPClients::SchemaRegistryClient. The registry's schemaType gives the type: JSON is JSON Schema, and none is Avro, which has no validator. Fetched versions are kept in Rails.cache; latest is always fetched. The client's time limits are its OPEN_TIMEOUT and TIMEOUT constants; a registry that can't be reached gives a 503 and an exception email.

  • A local schema registry, for development:

    docker run -ti -p 9092:9092 -p 8081:8081 redpandadata/redpanda
    
    # Register a schema file as the subject's next version
    jq -Rs '{schema: ., schemaType: "JSON"}' < <schema file> \
      | curl -X POST "http://localhost:8081/subjects/work-order-schema/versions" \
          -H 'Content-Type: application/vnd.schemaregistry.v1+json' \
          --data-binary @-
    
    # Read it back
    curl http://localhost:8081/subjects/work-order-schema/versions/latest
    

    Then run Sequencescape with SAPIO_SCHEMA_SOURCE=redpanda, and with the registry URL setting uncommented in development.rb.

  • Code: Api::V2::Sapio::OperationsController, ApiOperation (the api_operations table), ApiOperation::SchemaCheck, the Schemas classes and ApiOperation::CoordinatorJob, which processes each new operation in the background.