API package

The main modules within the API package are the Operations and Federation modules, which implement all the API routes, routing and federation logic. The Operations module directly implements the paths specified in federation.yaml OpenAPI 3 spec.

Operations Module

Federation Module

Provides methods to handle both local and federated requests

class candig_federation.api.federation.FederationResponse(request, url, endpoint_path, endpoint_payload, request_dict, endpoint_service, return_mimetype='application/json', timeout=5)[source]

Bases: object

This is a collection of methods to facilitate federated queries across the CanDIG network

Parameters
  • request (str) – The type of HTTP request to federate, either GET or POST. PUT TBD

  • url (str) – URL of the CanDIG service to be queried

  • endpoint_path (str) – Specific API endpoint of CanDIG service to be queried, may contain query string if GET

  • endpoint_payload (object, {param0=value0, paramN=valueN} for GET, JSON struct dependent on service endpoint for POST) – Query string or data needed by endpoint specified in endpoint_path

  • request_dict (Flask.Request) – Flask request object to be modified and forwarded along to service

  • service (str) – Name of CanDIG service to be queried, used for log tracking

  • return_mimetype (str) – HTTP content-type, default is application/json

  • timeout (int) – Wait time before a request times out, default is 5 seconds

announce_fed_in(source, code)[source]

Logging function to track requests received by Federation from CanDIG services

Parameters
  • source (str) – URL of service sending the response

  • code (int) – Response code

announce_fed_out(request_type, destination, path)[source]

Logging function to track requests being sent out by the Federation service

Parameters
  • request_type (str) – The type of HTTP request to federate, either GET or POST. PUT TBD

  • destination (str) – URL of service

  • path (Str) – API endpoint of service

async_requests(url_list, request, endpoint_path, endpoint_payload, endpoint_service, header)[source]

Send requests to each CanDIG node in the network asynchronously using FutureSession. The futures are returned back to and handled by handle_server_requests()

Parameters
  • url_list – List of peer server URLs

  • request (str) – The type of HTTP request to federate, either GET or POST. PUT TBD

  • endpoint_path (str) – Specific API endpoint of CanDIG service to be queried, may contain query string if GET

  • endpoint_payload (object, {param0=value0, paramN=valueN} for GET, JSON struct dependent on service endpoint for POST) – Query string or data needed by endpoint specified in endpoint_path

  • endpoint_service (str) – Specific microservice name, should match a service listed in services.json config

  • header (object) – Request headers defined in self.headers

Returns

List of Futures

federate_check()[source]

Checks if Federation conditions are met

Returns

Boolean

get_response_object()[source]

Driver method to communicate with other CanDIG nodes.

1. Check if federation is needed 1a. Broadcast if needed 2. If no federation is required, pass endpoint to service 3. Aggregate and return all the responses.

Returns

response_object, Status

Return type

object, int

get_server_from_url(server_url)[source]

Returns the server name from the server_url :param server_url: URL of service :type server_url: str :return: str

get_service(url, endpoint_path, endpoint_payload)[source]

Sends a GET request to service specified by url, adds response to self.status and self.results

Parameters
  • url – URL of service sending the response

  • endpoint_path (str) – Specific API endpoint of CanDIG service to be queried, may contain query string if GET

  • endpoint_payload (object, {param0=value0, paramN=valueN} for GET) – Query parameters needed by endpoint specified in endpoint_path

handle_server_request(request, endpoint_path, endpoint_payload, endpoint_service, header)[source]

Make peer server data requests and update the results and status for a FederationResponse

If a response from a peer server is received, it will be a Response Object with key pairs

{“status”: [], “message”: [], “results”: [], “service”: “name” }

The data structures within results are still unknown/undefined at this time, so just append everything instead of attempting to aggregate internal structs.

Parameters
  • request (str) – The type of HTTP request to federate, either GET or POST. PUT TBD

  • endpoint_path (str) – Specific API endpoint of CanDIG service to be queried, may contain query string if GET

  • endpoint_payload (object, {param0=value0, paramN=valueN} for GET, JSON struct dependent on service endpoint for POST) – Query string or data needed by endpoint specified in endpoint_path

  • endpoint_service (str) – Specific microservice name, should match a service listed in services.json config

  • header (object) – Request headers defined in self.headers

Returns

List of ResponseObjects, this specific return is used only in testing

merge_status(statuses)[source]

Returns a single status to represent the federated query.

Priority List: 1. Return 200 if one exists within the list 2. 201 > 405 > 504 > 404 > 500

Parameters

statuses (list) – List of integer response codes

Returns

Single response code

Return type

int

post_service(url, endpoint_path, endpoint_payload)[source]

Sends a POST request to service specified by url, adds response to self.status and self.results

Parameters
  • url – URL of service sending the response

  • endpoint_path (str) – Specific API endpoint of CanDIG service to be queried, may contain query string if GET

  • endpoint_payload (object, JSON struct dependent on service endpoint for POST) – Query parameters needed by endpoint specified in endpoint_path

Logging Module