Esempio n. 1
0
def get_api_spec(api_key, api_version="1.0", user_agent="civis-python"):
    """Download the Civis API specification.

    Parameters
    ----------
    api_key : str
        Your API key obtained from the Civis Platform.
    api_version : string, optional
        The version of endpoints to call. May instantiate multiple client
        objects with different versions.  Currently only "1.0" is supported.
    user_agent : string, optional
        Provide this user agent to the the Civis API, along with an
        API client version tag and ``requests`` version tag.
    """
    if api_version == "1.0":
        with open_session(api_key, MAX_RETRIES, user_agent=user_agent) as sess:
            response = sess.get("{}endpoints".format(get_base_url()))
    else:
        msg = "API specification for api version {} cannot be found"
        raise ValueError(msg.format(api_version))
    if response.status_code in (401, 403):
        msg = "{} error downloading API specification. API key may be expired."
        raise requests.exceptions.HTTPError(msg.format(response.status_code))
    response.raise_for_status()
    spec = response.json(object_pairs_hook=OrderedDict)
    return spec
Esempio n. 2
0
def get_api_spec(api_key, api_version="1.0"):
    """Download the Civis API specification.

    Parameters
    ----------
    api_key : str
        Your API key obtained from the Civis Platform.
    api_version : string, optional
        The version of endpoints to call. May instantiate multiple client
        objects with different versions.  Currently only "1.0" is supported.
    """
    civis_version = civis.__version__
    session = requests.Session()
    session.auth = (api_key, '')
    session_agent = session.headers.get('User-Agent', '')
    user_agent = "civis-python/{} {}".format(civis_version, session_agent)
    session.headers.update({"User-Agent": user_agent.strip()})
    max_retries = AggressiveRetry(MAX_RETRIES,
                                  backoff_factor=.75,
                                  status_forcelist=civis.civis.RETRY_CODES)
    adapter = HTTPAdapter(max_retries=max_retries)
    session.mount("https://", adapter)
    if api_version == "1.0":
        response = session.get("{}endpoints".format(get_base_url()))
    else:
        msg = "API specification for api version {} cannot be found"
        raise ValueError(msg.format(api_version))
    if response.status_code in (401, 403):
        msg = "{} error downloading API specification. API key may be expired."
        raise requests.exceptions.HTTPError(msg.format(response.status_code))
    response.raise_for_status()
    spec = response.json(object_pairs_hook=OrderedDict)
    return spec
Esempio n. 3
0
def get_swagger_spec(api_key, user_agent, api_version):
    session = requests.Session()
    session.auth = (api_key, '')
    session.headers.update({"User-Agent": user_agent.strip()})
    if api_version == "1.0":
        response = session.get("{}endpoints".format(get_base_url()))
    else:
        msg = "swagger spec for api version {} cannot be found"
        raise ValueError(msg.format(api_version))
    if response.status_code in (401, 403):
        msg = "{} error downloading API specification. API key may be expired."
        raise requests.exceptions.HTTPError(msg.format(response.status_code))
    response.raise_for_status()
    spec = response.json(object_pairs_hook=OrderedDict)
    return spec
Esempio n. 4
0
def get_swagger_spec(api_key, user_agent, api_version):
    session = requests.Session()
    session.auth = (api_key, '')
    session.headers.update({"User-Agent": user_agent.strip()})
    max_retries = AggressiveRetry(MAX_RETRIES,
                                  backoff_factor=.75,
                                  status_forcelist=civis.civis.RETRY_CODES)
    adapter = HTTPAdapter(max_retries=max_retries)
    session.mount("https://", adapter)
    if api_version == "1.0":
        response = session.get("{}endpoints".format(get_base_url()))
    else:
        msg = "swagger spec for api version {} cannot be found"
        raise ValueError(msg.format(api_version))
    if response.status_code in (401, 403):
        msg = "{} error downloading API specification. API key may be expired."
        raise requests.exceptions.HTTPError(msg.format(response.status_code))
    response.raise_for_status()
    spec = response.json(object_pairs_hook=OrderedDict)
    return spec
Esempio n. 5
0
def test_base_url_default():
    assert get_base_url() == 'https://api.civisanalytics.com/'
Esempio n. 6
0
def test_base_url_from_env():
    custom_url = 'https://api1.civisanalytics.com'
    with mock.patch.dict('os.environ', {'CIVIS_API_ENDPOINT': custom_url}):
        assert get_base_url() == custom_url + '/'