edx_api_utils.py 3.61 KB
Newer Older
1 2
"""Helper functions to get data from APIs"""
from __future__ import unicode_literals
3

4 5
import logging

6
from django.conf import settings
7
from django.core.cache import cache
8
from django.core.exceptions import ImproperlyConfigured
9
from edx_rest_api_client.client import EdxRestApiClient
10
from provider.oauth2.models import Client
11

12
from openedx.core.lib.cache_utils import zpickle, zunpickle
13
from openedx.core.lib.token_utils import JwtBuilder
14 15 16 17

log = logging.getLogger(__name__)


18 19 20 21
def get_fields(fields, response):
    """Extracts desired fields from the API response"""
    results = {}
    for field in fields:
22
        results[field] = response.get(field)
23
    return results
24 25


26
def get_edx_api_data(api_config, resource, api, resource_id=None, querystring=None, cache_key=None, many=True,
27
                     traverse_pagination=True, fields=None):
28 29 30
    """GET data from an edX REST API.

    DRY utility for handling caching and pagination.
31 32

    Arguments:
33 34 35 36
        api_config (ConfigurationModel): The configuration model governing interaction with the API.
        resource (str): Name of the API resource being requested.

    Keyword Arguments:
37
        api (APIClient): API client to use for requesting data.
38 39 40 41
        resource_id (int or str): Identifies a specific resource to be retrieved.
        querystring (dict): Optional query string parameters.
        cache_key (str): Where to cache retrieved data. The cache will be ignored if this is omitted
            (neither inspected nor updated).
42 43
        many (bool): Whether the resource requested is a collection of objects, or a single object.
            If false, an empty dict will be returned in cases of failure rather than the default empty list.
44
        traverse_pagination (bool): Whether to traverse pagination or return paginated response..
45 46

    Returns:
47 48
        Data returned by the API. When hitting a list endpoint, extracts "results" (list of dict)
        returned by DRF-powered APIs.
49
    """
50
    no_data = [] if many else {}
51 52

    if not api_config.enabled:
53
        log.warning('%s configuration is disabled.', api_config.API_NAME)
54 55
        return no_data

56
    if cache_key:
57
        cache_key = '{}.{}'.format(cache_key, resource_id) if resource_id is not None else cache_key
58
        cache_key += '.zpickled'
59

60
        cached = cache.get(cache_key)
61
        if cached:
62
            return zunpickle(cached)
63 64

    try:
65 66 67 68
        endpoint = getattr(api, resource)
        querystring = querystring if querystring else {}
        response = endpoint(resource_id).get(**querystring)

69
        if resource_id is not None:
70 71 72 73
            if fields:
                results = get_fields(fields, response)
            else:
                results = response
74
        elif traverse_pagination:
75
            results = _traverse_pagination(response, endpoint, querystring, no_data)
76 77
        else:
            results = response
78
    except:  # pylint: disable=bare-except
79
        log.exception('Failed to retrieve data from the %s API.', api_config.API_NAME)
80 81
        return no_data

82
    if cache_key:
83 84
        zdata = zpickle(results)
        cache.set(cache_key, zdata, api_config.cache_ttl)
85 86

    return results
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105


def _traverse_pagination(response, endpoint, querystring, no_data):
    """Traverse a paginated API response.

    Extracts and concatenates "results" (list of dict) returned by DRF-powered APIs.
    """
    results = response.get('results', no_data)

    page = 1
    next_page = response.get('next')
    while next_page:
        page += 1
        querystring['page'] = page
        response = endpoint.get(**querystring)
        results += response.get('results', no_data)
        next_page = response.get('next')

    return results