Skip to content

Commit

Permalink
WIP, authorize and get wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
pobocks committed Jan 18, 2018
1 parent 0ed038a commit 5a740f6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.pyc
30 changes: 30 additions & 0 deletions asnake/__init__.py
@@ -0,0 +1,30 @@
from requests import Session
from urllib.parse import urljoin, quote
import json

class ASnakeAuthError(Exception): pass

class ASnakeClient():
'''ArchivesSnake Client'''

def __init__(self, **config):
self.__dict__.update(config)
if not hasattr(self, 'session'): self.session = Session()
self.session.headers.update({'Accept': 'application/json',
'User-Agent': 'ArchivesSnake/0.1'})


def authorize(self, username, password):
resp = self.session.post(
urljoin(self.baseurl, f'users/{quote(username)}/login'),
params={"password": password, "expiring": False}
)

if resp.status_code != 200:
raise ASnakeAuthError("Failed to authorize ASnake")
else:
self.session.headers['X-ArchivesSpace-Session'] = json.loads(resp.text)['session']
return True

def get(self, url, *args, **kwargs):
return self.session.get(urljoin(self.baseurl, url), *args, **kwargs)
9 changes: 9 additions & 0 deletions setup.py
@@ -0,0 +1,9 @@
from setuptools import setup, find_packages

setup(
name="ArchivesSnake",
version="0.1",
packages=find_packages(),
zip_safe=False,
install_requires=["requests"]
)

0 comments on commit 5a740f6

Please sign in to comment.