API Calls from Python

In order to simplify making REST calls to the Portal REST API endpoints from within a Python plugin we provide an ApiClient

This class can be used to make calls to Portal endpoints. This is implemented efficiently as it doesn’t transfer data over the network, nor have to serialize and deserialize the data.

Example Usage:

from portal.api import client

query_doc = {
    "doc_types": ["collection"],
    "query": collections_query
}

result = client.put("/API/v2/search/",
    user=request.user,
    params={
        'page': page,
        'page_size': 15
    },
    json=query_doc
)

if result.status_code == 200:
    # data now contains the return value from the API endpoint
    data = result.data
    total_hits = data['hits']
    if total_hits:
        first_title = data['results'][0]['title']
portal.api.client.delete(url, user, **kwargs)

Make a DELETE request

Parameters:
  • url – A host-relative url
  • user – A string with a username or a django.contrib.auth.models.User object
  • kwargs – The same arguments request takes
Returns:

A HttpResponse object

Return type:

django.http.HttpResponse

portal.api.client.get(url, user, **kwargs)

Make a GET request

Parameters:
  • url – A host-relative url
  • user – A string with a username or a django.contrib.auth.models.User object
  • kwargs – The same arguments request takes
Returns:

A HttpResponse object

Return type:

django.http.HttpResponse

portal.api.client.head(url, user, **kwargs)

Make a HEAD request

Parameters:
  • url – A host-relative url
  • user – A string with a username or a django.contrib.auth.models.User object
  • kwargs – The same arguments request takes
Returns:

A HttpResponse object

Return type:

django.http.HttpResponse

portal.api.client.options(url, user, **kwargs)

Make a OPTIONS request

Parameters:
  • url – A host-relative url
  • user – A string with a username or a django.contrib.auth.models.User object
  • kwargs – The same arguments request takes
Returns:

A HttpResponse object

Return type:

django.http.HttpResponse

portal.api.client.patch(url, user, **kwargs)

Make a PATCH request

Parameters:
  • url – A host-relative url
  • user – A string with a username or a django.contrib.auth.models.User object
  • kwargs – The same arguments request takes
Returns:

A HttpResponse object

Return type:

django.http.HttpResponse

portal.api.client.post(url, user, **kwargs)

Make a POST request

Parameters:
  • url – A host-relative url
  • user – A string with a username or a django.contrib.auth.models.User object
  • kwargs – The same arguments request takes
Returns:

A HttpResponse object

Return type:

django.http.HttpResponse

portal.api.client.put(url, user, **kwargs)

Make a PUT request

Parameters:
  • url – A host-relative url
  • user – A string with a username or a django.contrib.auth.models.User object
  • kwargs – The same arguments request takes
Returns:

A HttpResponse object

Return type:

django.http.HttpResponse

portal.api.client.request(method, url, user, **kwargs)
Parameters:
  • method – The name of the HTTP method to call
  • url – A host-relative url
  • user – A string with a username or a django.contrib.auth.models.User object
  • params – (optional) a list of tuples or a dict which will be url-encoded and sent as query paramters
  • json – (optional) json data to be sent with the request
Returns:

A HttpResponse object

Return type:

django.http.HttpResponse