Example #1
0
    def test_put_request_exception(self, mock_request):
        mock_request.return_value = MockReturnValue(status=500)
        mock_request.side_effect = Exception("Custom error")

        with pytest.raises(utils.APIError) as e:
            utils.put_request(url)
        assert re.match("Custom error", str(e.value))
Example #2
0
def update_time_series(time_series: List[TimeSeries], **kwargs):
    """Update an existing time series.

    For each field that can be updated, a null value indicates that nothing should be done.

    Args:
        time_series (list[v05.dto.TimeSeries]):   List of time series data transfer objects to update.

    Keyword Args:
        api_key (str): Your api-key.

        project (str): Project name.

    Returns:
        An empty response.
    """

    api_key, project = config.get_config_variables(kwargs.get("api_key"),
                                                   kwargs.get("project"))
    url = config.get_base_url(
        api_version=0.5) + "/projects/{}/timeseries".format(project)

    body = {"items": [ts.__dict__ for ts in time_series]}

    headers = {
        "api-key": api_key,
        "content-type": "application/json",
        "accept": "application/json"
    }

    res = _utils.put_request(url, body=body, headers=headers)
    return res.json()
Example #3
0
    def test_put_request_ok(self, mock_request):
        mock_request.return_value = MockReturnValue(json_data=RESPONSE)

        response = utils.put_request(url, body=RESPONSE)
        response_json = response.json()

        assert response.status_code == 200
        assert len(response_json["data"]["items"]) == len(RESPONSE)
Example #4
0
def update_depth_series(depth_series: List[TimeSeries], **kwargs):
    """Update an existing time series.

    For each field that can be updated, a null value indicates that nothing should be done.

    Args:
        depth_series (list[v05.dto.TimeSeries]):   List of time series data transfer objects to update.

    Keyword Args:
        api_key (str): Your api-key.

        project (str): Project name.

    Returns:
        An empty response.
    """

    api_key, project = config.get_config_variables(kwargs.get("api_key"),
                                                   kwargs.get("project"))
    url = config.get_base_url(
        api_version=0.5) + "/projects/{}/timeseries".format(project)

    body = {"items": [ts.__dict__ for ts in depth_series]}

    headers = {
        "api-key": api_key,
        "content-type": "application/json",
        "accept": "application/json"
    }

    res = _utils.put_request(url, body=body, headers=headers)
    if res.json() == {}:
        for dsdto in depth_series:
            dsdto.name = _generateIndexName(dsdto.name)
            dsdto.isString = None
            dsdto.unit = None
        items = [
            ts.__dict__ for ts in depth_series if _has_depth_index_changes(ts)
        ]
        body = {"items": items}
        if len(items) > 0:
            res = _utils.put_request(url, body=body, headers=headers)
    return res.json()
Example #5
0
    def test_put_request_args(self, mock_request):
        import json
        def check_args_to_put_and_return_mock(arg_url, data=None, headers=None, params=None, cookies=None):
            # URL is sent as is
            assert arg_url == url
            # data is json encoded
            assert len(json.loads(data)["data"]["items"]) == len(RESPONSE)
            # cookies should be the same
            assert cookies == {"a-cookie": "a-cookie-val"}
            # Return the mock response
            return MockReturnValue(json_data=RESPONSE)

        mock_request.side_effect = check_args_to_put_and_return_mock

        response = utils.put_request(
            url, RESPONSE,
            headers={"Existing-Header": "SomeValue"},
            cookies={"a-cookie": "a-cookie-val"},
        )

        assert response.status_code == 200
Example #6
0
    def test_put_request_failed(self, mock_request):
        mock_request.return_value = MockReturnValue(status=400, json_data={"error": "Client error"})

        with pytest.raises(utils.APIError) as e:
            utils.put_request(url)
        assert re.match("Client error[\n]X-Request_id", str(e.value))

        mock_request.return_value = MockReturnValue(status=500, content="Server error")

        with pytest.raises(utils.APIError) as e:
            utils.put_request(url)
        assert re.match("Server error[\n]X-Request_id", str(e.value))

        mock_request.return_value = MockReturnValue(status=500, json_data={"error": "Server error"})

        with pytest.raises(utils.APIError) as e:
            utils.put_request(url)
        assert re.match("Server error[\n]X-Request_id", str(e.value))