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

Commit 54d0897

Browse files
committed
Merge branch 'release/0.4.0-dev0'
2 parents fb5d98b + 3344267 commit 54d0897

File tree

6 files changed

+234
-46
lines changed

6 files changed

+234
-46
lines changed

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM python:latest
2+
3+
WORKDIR /py-orbit-db-http-client
4+
RUN curl -L https://github.com/orbitdb/py-orbit-db-http-client/tarball/develop | tar -xz --strip-components 1 \
5+
&& pip install .

orbitdbapi/client.py

Lines changed: 21 additions & 13 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,28 +43,33 @@ 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:
4851
self.logger.warning('Json decode error', exc_info=True)
49-
self.logger.debug(res.text)
52+
self.logger.log(15, res.text)
5053
raise
5154
try:
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

62-
def db(self, dbname, **kwargs):
63-
return DB(self, self.open_db(dbname, **kwargs), **self.__config)
65+
def db(self, dbname, local_options=None, **kwargs):
66+
if local_options is None: local_options = {}
67+
return DB(self, self.open_db(dbname, **kwargs), **{**self.__config, **local_options})
6468

6569
def open_db(self, dbname, **kwargs):
6670
endpoint = '/'.join(['db', urlquote(dbname, safe='')])
67-
return self._call('post', endpoint, kwargs)
71+
return self._call('POST', endpoint, **kwargs)
72+
73+
def searches(self):
74+
endpoint = '/'.join(['peers', 'searches'])
75+
return self._call('GET', endpoint)

orbitdbapi/db.py

Lines changed: 62 additions & 30 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):
@@ -36,6 +37,10 @@ def cache_remove(self, item):
3637
if item in self.__cache:
3738
del self.__cache[item]
3839

40+
@property
41+
def cached(self):
42+
return self.__use_cache
43+
3944
@property
4045
def index_by(self):
4146
return self.__index_by
@@ -52,36 +57,45 @@ def params(self):
5257
def dbname(self):
5358
return self.__dbname
5459

60+
@property
61+
def id(self):
62+
return self.__id
63+
5564
@property
5665
def dbtype(self):
5766
return self.__type
5867

68+
@property
69+
def capabilities(self):
70+
return deepcopy(self.__params.get('capabilities', []))
71+
5972
@property
6073
def queryable(self):
61-
return 'query' in self.__params.get('capabilities', {})
74+
return 'query' in self.__params.get('capabilities', [])
75+
6276
@property
6377
def putable(self):
64-
return 'put' in self.__params.get('capabilities', {})
78+
return 'put' in self.__params.get('capabilities', [])
6579

6680
@property
6781
def removeable(self):
68-
return 'remove' in self.__params.get('capabilities', {})
82+
return 'remove' in self.__params.get('capabilities', [])
6983

7084
@property
7185
def iterable(self):
72-
return 'iterator' in self.__params.get('capabilities', {})
86+
return 'iterator' in self.__params.get('capabilities', [])
7387

7488
@property
7589
def addable(self):
76-
return 'add' in self.__params.get('capabilities', {})
90+
return 'add' in self.__params.get('capabilities', [])
7791

7892
@property
7993
def valuable(self):
80-
return 'value' in self.__params.get('capabilities', {})
94+
return 'value' in self.__params.get('capabilities', [])
8195

8296
@property
8397
def incrementable(self):
84-
return 'inc' in self.__params.get('capabilities', {})
98+
return 'inc' in self.__params.get('capabilities', [])
8599

86100
@property
87101
def indexed(self):
@@ -97,7 +111,7 @@ def write_access(self):
97111

98112
def info(self):
99113
endpoint = '/'.join(['db', self.__id_safe])
100-
return self.__client._call('get', endpoint)
114+
return self.__client._call('GET', endpoint)
101115

102116
def get(self, item, cache=None, unpack=False):
103117
if cache is None: cache = self.__use_cache
@@ -106,7 +120,7 @@ def get(self, item, cache=None, unpack=False):
106120
result = self.__cache[item]
107121
else:
108122
endpoint = '/'.join(['db', self.__id_safe, item])
109-
result = self.__client._call('get', endpoint)
123+
result = self.__client._call('GET', endpoint)
110124
if cache: self.__cache[item] = result
111125
if isinstance(result, Hashable): return deepcopy(result)
112126
if isinstance(result, Iterable): return deepcopy(result)
@@ -117,13 +131,13 @@ def get(self, item, cache=None, unpack=False):
117131

118132
def get_raw(self, item):
119133
endpoint = '/'.join(['db', self.__id_safe, 'raw', str(item)])
120-
return (self.__client._call('get', endpoint))
134+
return (self.__client._call('GET', endpoint))
121135

122136
def put(self, item, cache=None):
123137
if self.__enforce_caps and not self.putable:
124-
raise CapabilityError('Db {} does not have put capability'.format(self.__dbname))
125-
if self.indexed and (not hasattr(item, self.__index_by)) and self.__enforce_indexby:
126-
raise MissingIndexError("The provided document doesn't contain field '{}'".format(self.__index_by))
138+
raise CapabilityError(f'Db {self.__dbname} does not have put capability')
139+
if self.indexed and (not self.__index_by in item) and self.__enforce_indexby:
140+
raise MissingIndexError(f"The provided document {item} doesn't contain field '{self.__index_by}'")
127141

128142
if cache is None: cache = self.__use_cache
129143
if cache:
@@ -134,59 +148,77 @@ def put(self, item, cache=None):
134148
if index_val:
135149
self.__cache[index_val] = item
136150
endpoint = '/'.join(['db', self.__id_safe, 'put'])
137-
entry_hash = self.__client._call('post', endpoint, item).get('hash')
151+
entry_hash = self.__client._call('POST', endpoint, json=item).get('hash')
138152
if cache and entry_hash: self.__cache[entry_hash] = item
139153
return entry_hash
140154

141155
def add(self, item, cache=None):
142156
if self.__enforce_caps and not self.addable:
143-
raise CapabilityError('Db {} does not have add capability'.format(self.__dbname))
157+
raise CapabilityError(f'Db {self.__dbname} does not have add capability')
144158
if cache is None: cache = self.__use_cache
145159
endpoint = '/'.join(['db', self.__id_safe, 'add'])
146-
entry_hash = self.__client._call('post', endpoint, item).get('hash')
160+
entry_hash = self.__client._call('POST', endpoint, json=item).get('hash')
147161
if cache and entry_hash: self.__cache[entry_hash] = item
148162
return entry_hash
149163

164+
def inc(self, val):
165+
val = int(val)
166+
endpoint = '/'.join(['db', self.__id_safe, 'inc'])
167+
return self.__client._call('POST', endpoint, json={'val':val})
168+
169+
def value(self):
170+
endpoint = '/'.join(['db', self.__id_safe, 'value'])
171+
return self.__client._call('GET', endpoint)
172+
150173
def iterator_raw(self, **kwargs):
151174
if self.__enforce_caps and not self.iterable:
152-
raise CapabilityError('Db {} does not have iterator capability'.format(self.__dbname))
175+
raise CapabilityError(f'Db {self.__dbname} does not have iterator capability')
153176
endpoint = '/'.join(['db', self.__id_safe, 'rawiterator'])
154-
return self.__client._call('get', endpoint, kwargs)
177+
return self.__client._call('GET', endpoint, json=kwargs)
155178

156179
def iterator(self, **kwargs):
157180
if self.__enforce_caps and not self.iterable:
158-
raise CapabilityError('Db {} does not have iterator capability'.format(self.__dbname))
181+
raise CapabilityError(f'Db {self.__dbname} does not have iterator capability')
159182
endpoint = '/'.join(['db', self.__id_safe, 'iterator'])
160-
return self.__client._call('get', endpoint, kwargs)
183+
return self.__client._call('GET', endpoint, json=kwargs)
161184

162185
def index(self):
163186
endpoint = '/'.join(['db', self.__id_safe, 'index'])
164-
result = self.__client._call('get', endpoint)
187+
result = self.__client._call('GET', endpoint)
165188
return result
166189

167190
def all(self):
168191
endpoint = '/'.join(['db', self.__id_safe, 'all'])
169-
result = self.__client._call('get', endpoint)
192+
result = self.__client._call('GET', endpoint)
170193
if isinstance(result, Hashable):
171194
self.__cache = result
172195
return result
173196

174197
def remove(self, item):
175198
if self.__enforce_caps and not self.removeable:
176-
raise CapabilityError('Db {} does not have remove capability'.format(self.__dbname))
199+
raise CapabilityError(f'Db {self.__dbname} does not have remove capability')
177200
item = str(item)
178201
endpoint = '/'.join(['db', self.__id_safe, item])
179-
return self.__client._call('delete', endpoint)
202+
return self.__client._call('DELETE', endpoint)
180203

181204
def unload(self):
182205
endpoint = '/'.join(['db', self.__id_safe])
183-
return self.__client._call('delete', endpoint)
206+
return self.__client._call('DELETE', endpoint)
184207

185208
def events(self, eventname):
186209
endpoint = '/'.join(['db', self.__id_safe, 'events', urlquote(eventname, safe='')])
187-
#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()
210+
res = self.__client._call_raw('GET', endpoint, stream=True)
211+
res.raise_for_status()
212+
return SSEClient(res.stream()).events()
213+
214+
def findPeers(self, **kwargs):
215+
endpoint = '/'.join(['peers','searches','db', self.__id_safe])
216+
return self.__client._call('POST', endpoint, json=kwargs)
217+
218+
def getPeers(self):
219+
endpoint = '/'.join(['db', self.__id_safe, 'peers'])
220+
return self.__client._call('GET', endpoint)
221+
190222

191223
class CapabilityError(Exception):
192224
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)