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

Commit b96e607

Browse files
committed
0.1.0.rc.1.dev
1 parent ed2ed43 commit b96e607

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
106+
# custom
107+
debug/

orbitdbapi/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = '0.1.0.rc.1.dev'

orbitdbapi/client.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import json
2+
import logging
3+
import requests
4+
from .db import DB
5+
from urllib.parse import quote as urlquote
6+
7+
class OrbitDbAPI ():
8+
def __init__ (self, **kwargs):
9+
self.logger = logging.getLogger(__name__)
10+
self.__config = kwargs
11+
self.__base_url = self.__config.get('base_url')
12+
self.__use_db_cache = self.__config.get('use_db_cache')
13+
14+
15+
def _do_request(self, *args, **kwargs):
16+
try:
17+
return requests.request(*args, **kwargs)
18+
except:
19+
self.logger.exception('Exception during api call')
20+
raise
21+
22+
def _call(self, method, endpoint, body=None):
23+
url = '/'.join([self.__base_url, endpoint])
24+
res = self._do_request(method, url, json=body)
25+
try:
26+
result = res.json()
27+
except:
28+
self.logger.warning('Json decode error', exc_info=True)
29+
self.logger.debug(res.text)
30+
raise
31+
try:
32+
res.raise_for_status()
33+
except:
34+
self.logger.exception('Server Error')
35+
self.logger.debug(result)
36+
raise
37+
return result
38+
39+
def list_dbs(self):
40+
return self._call('get', 'dbs')
41+
42+
def db(self, dbname, params=None):
43+
return DB(self, self.open_db(urlquote(dbname, safe=''), params), **self.__config)
44+
45+
def open_db(self, dbname, params=None):
46+
endpoint = '/'.join(['db', dbname])
47+
return self._call('post', endpoint, params)

orbitdbapi/db.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import json
2+
import logging
3+
from copy import deepcopy
4+
from collections.abc import Hashable, Iterable
5+
from urllib.parse import quote as urlquote
6+
7+
8+
class DB ():
9+
def __init__(self, client, params, **kwargs):
10+
self.__cache = {}
11+
self.__client = client
12+
self.__params = params
13+
self.__dbname = params['dbname']
14+
self.__id = params['id']
15+
self.__id_safe = urlquote(self.__id, safe='')
16+
self.__type = params['type']
17+
self.__use_cache = kwargs.get('use_db_cache')
18+
self.logger = logging.getLogger(__name__)
19+
20+
if hasattr( self.params, 'indexBy'):
21+
self.index_by = self.__params.indexBy
22+
23+
def clear_cache(self):
24+
self.__cache = {}
25+
26+
def cache_get(self, item):
27+
item = str(item)
28+
return deepcopy(self.__cache.get(item))
29+
30+
def cache_remove(self, item):
31+
item = str(item)
32+
if item in self.__cache:
33+
del self.__cache[item]
34+
35+
@property
36+
def cache(self):
37+
return deepcopy(self.__cache)
38+
39+
@property
40+
def params(self):
41+
return deepcopy(self.__params)
42+
43+
@property
44+
def dbname(self):
45+
return self.__dbname
46+
47+
@property
48+
def dbtype(self):
49+
return self.__type
50+
51+
def info(self):
52+
endpoint = '/'.join(['db', self.__id_safe])
53+
return self.__client._call('get', endpoint)
54+
55+
def get(self, item, cache=None):
56+
if cache is None: cache = self.__use_cache
57+
item = str(item)
58+
if cache and item in self.__cache:
59+
result = deepcopy(self.__cache[item])
60+
else:
61+
endpoint = '/'.join(['db', self.__id_safe, item])
62+
result = self.__client._call('get', endpoint)
63+
if cache: self.__cache[item] = result
64+
if isinstance(result, Hashable): return deepcopy(result)
65+
#if isinstance(result, Iterable): return deepcopy(next(result, {}))
66+
#if isinstance(result, list): return deepcopy(next(iter(result), {}))
67+
return result
68+
69+
def get_raw(self, item):
70+
endpoint = '/'.join(['db', self.__id_safe, 'raw', str(item)])
71+
return (self.__client._call('get', endpoint))
72+
73+
def put(self, item, cache=None):
74+
if cache is None: cache = self.__use_cache
75+
if cache:
76+
if hasattr(self, 'index_by'):
77+
if hasattr(item, self.index_by):
78+
index_val = getattr(item, self.index_by)
79+
else:
80+
index_val = item[self.index_by]
81+
self.__cache[index_val] = item
82+
endpoint = '/'.join(['db', self.__id_safe, 'put'])
83+
entry_hash = self.__client._call('post', endpoint, item)
84+
if cache: self.__cache[entry_hash] = item
85+
return entry_hash
86+
87+
def add(self, item, cache=None):
88+
if cache is None: cache = self.__use_cache
89+
endpoint = '/'.join(['db', self.__id_safe, 'add'])
90+
entry_hash = self.__client._call('post', endpoint, item)
91+
if cache: self.__cache[entry_hash] = item
92+
return entry_hash
93+
94+
def iterator_raw(self, params):
95+
endpoint = '/'.join(['db', self.__id_safe, 'rawiterator'])
96+
return self.__client._call('get', endpoint, params)
97+
98+
def iterator(self, params):
99+
endpoint = '/'.join(['db', self.__id_safe, 'iterator'])
100+
return self.__client._call('get', endpoint, params)
101+
102+
def index(self):
103+
endpoint = '/'.join(['db', self.__id_safe, 'index'])
104+
return self.__client._call('get', endpoint)
105+
106+
def unload(self):
107+
endpoint = '/'.join(['db', self.__id_safe])
108+
return self.__client._call('delete', endpoint)

setup.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
3+
from orbitdbapi import __version__
4+
from setuptools import setup, find_packages
5+
6+
setup(
7+
name='orbitdbapi',
8+
version=__version__,
9+
description='A Python HTTP Orbitdb API Client',
10+
author='Phillip Mackintosh',
11+
url='https://github.com/phillmac/py-orbit-db-http-client',
12+
packages=find_packages(),
13+
install_requires=[
14+
'requests >= 2.11'
15+
],
16+
classifiers=[
17+
'Programming Language :: Python :: 3',
18+
'License :: OSI Approved :: MIT License',
19+
'Operating System :: OS Independent',
20+
],
21+
)

0 commit comments

Comments
 (0)