Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit 4fe2e8c

Browse files
committed
Use httpx instead of hyper
1 parent e8f4cfd commit 4fe2e8c

File tree

4 files changed

+49
-40
lines changed

4 files changed

+49
-40
lines changed

orbitdbapi/client.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import json
22
import logging
3-
import requests
4-
from .db import DB
5-
from hypertemp.contrib import HTTP20Adapter
3+
from pprint import pformat
64
from urllib.parse import quote as urlquote
75

6+
import httpx
7+
8+
from .db import DB
9+
10+
811
class OrbitDbAPI ():
912
def __init__ (self, **kwargs):
1013
self.logger = logging.getLogger(__name__)
1114
self.__config = kwargs
1215
self.__base_url = self.__config.get('base_url')
1316
self.__use_db_cache = self.__config.get('use_db_cache', True)
1417
self.__timeout = self.__config.get('timeout', 30)
15-
self.__session = requests.Session()
16-
self.__session.mount(self.__base_url, HTTP20Adapter(timeout=self.__timeout))
18+
self.__session = httpx.Client()
1719
self.logger.debug('Base url: ' + self.__base_url)
1820

1921
@property
@@ -29,6 +31,7 @@ def use_db_cache(self):
2931
return self.__use_db_cache
3032

3133
def _do_request(self, *args, **kwargs):
34+
self.logger.log(15, json.dumps([args, kwargs]))
3235
kwargs['timeout'] = kwargs.get('timeout', self.__timeout)
3336
try:
3437
return self.__session.request(*args, **kwargs)
@@ -40,8 +43,8 @@ def _call_raw(self, method, endpoint, **kwargs):
4043
url = '/'.join([self.__base_url, endpoint])
4144
return self._do_request(method, url, **kwargs)
4245

43-
def _call(self, method, endpoint, body=None):
44-
res = self._call_raw(method, endpoint, json=body)
46+
def _call(self, method, endpoint, **kwargs):
47+
res = self._call_raw(method, endpoint, **kwargs)
4548
try:
4649
result = res.json()
4750
except:
@@ -52,16 +55,16 @@ def _call(self, method, endpoint, body=None):
5255
res.raise_for_status()
5356
except:
5457
self.logger.exception('Server Error')
55-
self.logger.debug(result)
58+
self.logger.error(pformat(result))
5659
raise
5760
return result
5861

5962
def list_dbs(self):
60-
return self._call('get', 'dbs')
63+
return self._call('GET', 'dbs')
6164

6265
def db(self, dbname, **kwargs):
6366
return DB(self, self.open_db(dbname, **kwargs), **self.__config)
6467

6568
def open_db(self, dbname, **kwargs):
6669
endpoint = '/'.join(['db', urlquote(dbname, safe='')])
67-
return self._call('post', endpoint, kwargs)
70+
return self._call('POST', endpoint, **kwargs)

orbitdbapi/db.py

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import json
22
import logging
3-
from copy import deepcopy
4-
from sseclient import SSEClient
53
from collections.abc import Hashable, Iterable
4+
from copy import deepcopy
65
from urllib.parse import quote as urlquote
76

7+
from sseclient import SSEClient
8+
89

910
class DB ():
1011
def __init__(self, client, params, **kwargs):
@@ -56,32 +57,37 @@ def dbname(self):
5657
def dbtype(self):
5758
return self.__type
5859

60+
@property
61+
def capabilities(self):
62+
return deepcopy(self.__params.get('capabilities', []))
63+
5964
@property
6065
def queryable(self):
61-
return 'query' in self.__params.get('capabilities', {})
66+
return 'query' in self.__params.get('capabilities', [])
67+
6268
@property
6369
def putable(self):
64-
return 'put' in self.__params.get('capabilities', {})
70+
return 'put' in self.__params.get('capabilities', [])
6571

6672
@property
6773
def removeable(self):
68-
return 'remove' in self.__params.get('capabilities', {})
74+
return 'remove' in self.__params.get('capabilities', [])
6975

7076
@property
7177
def iterable(self):
72-
return 'iterator' in self.__params.get('capabilities', {})
78+
return 'iterator' in self.__params.get('capabilities', [])
7379

7480
@property
7581
def addable(self):
76-
return 'add' in self.__params.get('capabilities', {})
82+
return 'add' in self.__params.get('capabilities', [])
7783

7884
@property
7985
def valuable(self):
80-
return 'value' in self.__params.get('capabilities', {})
86+
return 'value' in self.__params.get('capabilities', [])
8187

8288
@property
8389
def incrementable(self):
84-
return 'inc' in self.__params.get('capabilities', {})
90+
return 'inc' in self.__params.get('capabilities', [])
8591

8692
@property
8793
def indexed(self):
@@ -97,7 +103,7 @@ def write_access(self):
97103

98104
def info(self):
99105
endpoint = '/'.join(['db', self.__id_safe])
100-
return self.__client._call('get', endpoint)
106+
return self.__client._call('GET', endpoint)
101107

102108
def get(self, item, cache=None, unpack=False):
103109
if cache is None: cache = self.__use_cache
@@ -106,7 +112,7 @@ def get(self, item, cache=None, unpack=False):
106112
result = self.__cache[item]
107113
else:
108114
endpoint = '/'.join(['db', self.__id_safe, item])
109-
result = self.__client._call('get', endpoint)
115+
result = self.__client._call('GET', endpoint)
110116
if cache: self.__cache[item] = result
111117
if isinstance(result, Hashable): return deepcopy(result)
112118
if isinstance(result, Iterable): return deepcopy(result)
@@ -117,11 +123,11 @@ def get(self, item, cache=None, unpack=False):
117123

118124
def get_raw(self, item):
119125
endpoint = '/'.join(['db', self.__id_safe, 'raw', str(item)])
120-
return (self.__client._call('get', endpoint))
126+
return (self.__client._call('GET', endpoint))
121127

122128
def put(self, item, cache=None):
123129
if self.__enforce_caps and not self.putable:
124-
raise CapabilityError('Db {} does not have put capability'.format(self.__dbname))
130+
raise CapabilityError(f'Db {self.__dbname} does not have put capability')
125131
if self.indexed and (not hasattr(item, self.__index_by)) and self.__enforce_indexby:
126132
raise MissingIndexError("The provided document doesn't contain field '{}'".format(self.__index_by))
127133

@@ -134,59 +140,60 @@ def put(self, item, cache=None):
134140
if index_val:
135141
self.__cache[index_val] = item
136142
endpoint = '/'.join(['db', self.__id_safe, 'put'])
137-
entry_hash = self.__client._call('post', endpoint, item).get('hash')
143+
entry_hash = self.__client._call('POST', endpoint, item).get('hash')
138144
if cache and entry_hash: self.__cache[entry_hash] = item
139145
return entry_hash
140146

141147
def add(self, item, cache=None):
142148
if self.__enforce_caps and not self.addable:
143-
raise CapabilityError('Db {} does not have add capability'.format(self.__dbname))
149+
raise CapabilityError(f'Db {self.__dbname} does not have add capability')
144150
if cache is None: cache = self.__use_cache
145151
endpoint = '/'.join(['db', self.__id_safe, 'add'])
146-
entry_hash = self.__client._call('post', endpoint, item).get('hash')
152+
entry_hash = self.__client._call('POST', endpoint, item).get('hash')
147153
if cache and entry_hash: self.__cache[entry_hash] = item
148154
return entry_hash
149155

150156
def iterator_raw(self, **kwargs):
151157
if self.__enforce_caps and not self.iterable:
152-
raise CapabilityError('Db {} does not have iterator capability'.format(self.__dbname))
158+
raise CapabilityError(f'Db {self.__dbname} does not have iterator capability')
153159
endpoint = '/'.join(['db', self.__id_safe, 'rawiterator'])
154-
return self.__client._call('get', endpoint, kwargs)
160+
return self.__client._call('GET', endpoint, json=kwargs)
155161

156162
def iterator(self, **kwargs):
157163
if self.__enforce_caps and not self.iterable:
158-
raise CapabilityError('Db {} does not have iterator capability'.format(self.__dbname))
164+
raise CapabilityError(f'Db {self.__dbname} does not have iterator capability')
159165
endpoint = '/'.join(['db', self.__id_safe, 'iterator'])
160-
return self.__client._call('get', endpoint, kwargs)
166+
return self.__client._call('GET', endpoint, json=kwargs)
161167

162168
def index(self):
163169
endpoint = '/'.join(['db', self.__id_safe, 'index'])
164-
result = self.__client._call('get', endpoint)
170+
result = self.__client._call('GET', endpoint)
165171
return result
166172

167173
def all(self):
168174
endpoint = '/'.join(['db', self.__id_safe, 'all'])
169-
result = self.__client._call('get', endpoint)
175+
result = self.__client._call('GET', endpoint)
170176
if isinstance(result, Hashable):
171177
self.__cache = result
172178
return result
173179

174180
def remove(self, item):
175181
if self.__enforce_caps and not self.removeable:
176-
raise CapabilityError('Db {} does not have remove capability'.format(self.__dbname))
182+
raise CapabilityError(f'Db {self.__dbname} does not have remove capability')
177183
item = str(item)
178184
endpoint = '/'.join(['db', self.__id_safe, item])
179-
return self.__client._call('delete', endpoint)
185+
return self.__client._call('DELETE', endpoint)
180186

181187
def unload(self):
182188
endpoint = '/'.join(['db', self.__id_safe])
183-
return self.__client._call('delete', endpoint)
189+
return self.__client._call('DELETE', endpoint)
184190

185191
def events(self, eventname):
186192
endpoint = '/'.join(['db', self.__id_safe, 'events', urlquote(eventname, safe='')])
187193
#return SSEClient('{}/{}'.format(self.__client.base_url, endpoint), session=self.__client.session)
188-
req = self.__client._call_raw('get', endpoint, stream=True)
189-
return SSEClient(req).events()
194+
res = self.__client._call_raw('GET', endpoint, stream=True)
195+
res.raise_for_status()
196+
return SSEClient(res.stream()).events()
190197

191198
class CapabilityError(Exception):
192199
pass

orbitdbapi/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version = '0.3.0-dev0'
1+
version = '0.4.0-dev0'
22
version_info = tuple([int(d) for d in version.split("-")[0].split(".")])

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
url='https://github.com/phillmac/py-orbit-db-http-client',
1414
packages=find_packages(),
1515
install_requires=[
16-
'requests >= 2.11',
17-
'hypertemp == 0.8.0',
16+
'httpx >= 0.7.5',
1817
'sseclient==0.0.24'
1918
],
2019
classifiers=[

0 commit comments

Comments
 (0)