Changeset View
Changeset View
Standalone View
Standalone View
swh/core/api/__init__.py
# Copyright (C) 2015-2017 The Software Heritage developers | # Copyright (C) 2015-2017 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import collections | from collections import abc | ||||
import functools | import functools | ||||
import inspect | import inspect | ||||
import json | import json | ||||
import logging | import logging | ||||
import pickle | import pickle | ||||
import requests | import requests | ||||
import datetime | import datetime | ||||
▲ Show 20 Lines • Show All 170 Lines • ▼ Show 20 Lines | def raw_verb(self, verb, endpoint, **opts): | ||||
return getattr(self.session, verb)( | return getattr(self.session, verb)( | ||||
self._url(endpoint), | self._url(endpoint), | ||||
**opts | **opts | ||||
) | ) | ||||
except requests.exceptions.ConnectionError as e: | except requests.exceptions.ConnectionError as e: | ||||
raise self.api_exception(e) | raise self.api_exception(e) | ||||
def post(self, endpoint, data, **opts): | def post(self, endpoint, data, **opts): | ||||
if isinstance(data, (collections.Iterator, collections.Generator)): | if isinstance(data, (abc.Iterator, abc.Generator)): | ||||
data = (encode_data(x) for x in data) | data = (encode_data(x) for x in data) | ||||
else: | else: | ||||
data = encode_data(data) | data = encode_data(data) | ||||
chunk_size = opts.pop('chunk_size', self.chunk_size) | chunk_size = opts.pop('chunk_size', self.chunk_size) | ||||
response = self.raw_verb( | response = self.raw_verb( | ||||
'post', endpoint, data=data, | 'post', endpoint, data=data, | ||||
headers={'content-type': 'application/x-msgpack', | headers={'content-type': 'application/x-msgpack', | ||||
'accept': 'application/x-msgpack'}, | 'accept': 'application/x-msgpack'}, | ||||
▲ Show 20 Lines • Show All 148 Lines • Show Last 20 Lines |