Skip to content

Commit 85a709d

Browse files
authored
Merge pull request #6 from Web-Power/futureproofing
Futureproofing: api framework
2 parents 309925f + 4c2cb85 commit 85a709d

7 files changed

Lines changed: 67 additions & 9 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ testtestinstall:
104104
# -m venv is always there, conda maybe not.
105105
python3 -m venv $(TESTENV)
106106
# Note: cd first to prevent the current directory to be found as valid module.
107-
cd $(TESTENV) && $(TESTENV)/bin/python3 -m pip install --upgrade --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple pydundas
107+
cd $(TESTENV) && $(TESTENV)/bin/python3 -m pip install --no-cache-dir --upgrade --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple pydundas
108108
@echo Module version: $(shell $(PY3) -c 'from pydundas import __version__ as v; print(v)').
109109
@echo Version on test Pypi: $(shell curl -s 'https://test.pypi.org/pypi/pydundas/json' | jq -r '.info.version').
110110
@echo Version in test env: $(shell cd $(TESTENV) && $(TESTENV)/bin/pip3 freeze | grep -i pydundas).
@@ -124,7 +124,7 @@ testinstall:
124124
# -m venv is always there, conda maybe not.
125125
python3 -m venv $(TESTENV)
126126
# Note: cd first to prevent the current directory to be found as valid module.
127-
cd $(TESTENV) && $(TESTENV)/bin/python3 -m pip install --upgrade pydundas
127+
cd $(TESTENV) && $(TESTENV)/bin/python3 -m pip install --no-cache-dir --upgrade pydundas
128128
@echo Module version: $(shell $(PY3) -c 'from pydundas import __version__ as v; print(v)').
129129
@echo Version on Pypi: $(shell curl -s 'https://test.pypi.org/pypi/pydundas/json' | jq -r '.info.version').
130130
@echo Version in test env: $(shell cd $(TESTENV) && $(TESTENV)/bin/pip3 freeze | grep -i pydundas).

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ out, it will be done for you, in all cases if you so wish.
1616

1717
# Why this module is useful
1818

19-
It currently does 2 things for you.
19+
It currently does 3 things for you.
2020

2121
If you use `dundas.Session` within a [context manager](https://docs.python.org/3/reference/datamodel.html#context-managers),
2222
the context manager wil log you in and out automagically, no matter what happens. You can
@@ -27,6 +27,9 @@ Each and every call to the API needs to have the same `sessionId` parameter. Thi
2727
shortcuts for you for `get`, `post` and `delete`, to make your life easier. You do not need
2828
to repeat the host, api path prefix or sessionId every single time.
2929

30+
Some API calls are ported and might have helper methods. I am updating the module based on what I
31+
need and use, so I do not expect to have everything ported on my own.
32+
3033
# Installation
3134

3235
Simply with pip:
@@ -174,6 +177,18 @@ The same would happen if you reuse an object after logging out.
174177

175178
I'm not that mean and I won't burn through your elastic hours, but be careful and that's why context the manager is awesome.
176179

180+
# API calls
181+
182+
For example, to find the ID of a project:
183+
```python
184+
from pydundas import Api, Session, creds_from_yaml
185+
186+
with Session(**creds_from_yaml('credentials.yaml')) as d:
187+
a=Api(d)
188+
project = a.project()
189+
print(project.getProjectIDByName('DP'))
190+
```
191+
177192
# Develop
178193

179194
You can either use `conda` or `virtualenv`. Most relevant commands are in the Makefile.

pydundas/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
__version__ = '1.2'
1+
__version__ = '1.3.1'
22

33
# Nicer namespace for the caller
44
from .dundas import Session, creds_from_yaml
5+
from .rest.api import Api as Api

pydundas/dundas.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self, *, user, pwd, url, loglevel=None):
3838

3939
self.user, self.pwd = user, pwd
4040
self.url = url if url.startswith('http') else 'https://{}'.format(url)
41-
self.api = self.url + '/api/'
41+
self.endpoint = self.url + '/api/'
4242

4343
# Will be setup within login()
4444
self.session_id = None
@@ -93,7 +93,7 @@ def login(self):
9393
}
9494
self.logger.info('Logging in.')
9595
# We can't use self.post() yet as session_id is not set.
96-
r = self.s.post(self.api + 'logon/', json=login_data)
96+
r = self.s.post(self.endpoint + 'logon/', json=login_data)
9797
# The following line exceptions out on not 200 return code.
9898
r.raise_for_status()
9999

@@ -122,17 +122,28 @@ def is_loggedin(self):
122122

123123
# All get/post/delete will have the same url base, and will all require the same
124124
# params parameter. Let's make everybody's life easy.
125+
# A query could legitimately need to extend the 'params' parameter, hence the merge of kwargs
126+
# with the hardcoded dict with sessionId.
127+
128+
def extend_with_sessionid(self, kwargs):
129+
if 'params' in kwargs:
130+
kwargs['params']['sessionId'] = self.session_id
131+
else:
132+
kwargs['params'] = {'sessionId': self.session_id}
133+
return kwargs
134+
135+
125136
def get(self, url, **kwargs):
126-
r = self.s.get(self.api + url, params={'sessionId': self.session_id}, **kwargs)
137+
r = self.s.get(self.endpoint + url, **self.extend_with_sessionid(kwargs))
127138
r.raise_for_status()
128139
return r
129140

130141
def post(self, url, **kwargs):
131-
r = self.s.post(self.api + url, params={'sessionId': self.session_id}, **kwargs)
142+
r = self.s.post(self.endpoint + url, **self.extend_with_sessionid(kwargs))
132143
r.raise_for_status()
133144
return r
134145

135146
def delete(self, url, **kwargs):
136-
r = self.s.delete(self.api + url, params={'sessionId': self.session_id}, **kwargs)
147+
r = self.s.delete(self.endpoint + url, **self.extend_with_sessionid(kwargs))
137148
r.raise_for_status()
138149
return r

pydundas/rest/__init__.py

Whitespace-only changes.

pydundas/rest/api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Api:
2+
3+
def __init__(self, session):
4+
self.session = session
5+
self._project = None
6+
7+
def project(self):
8+
if not self._project:
9+
from .project import Project
10+
self._project = Project(self.session)
11+
12+
return self._project

pydundas/rest/project.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class ProjectNotFound(Exception):
2+
pass
3+
4+
5+
class Project:
6+
7+
def __init__(self, session):
8+
self.session = session
9+
10+
def getProjectIDByName(self, name):
11+
ll = list(filter(lambda x: x['objectType'] == 'Project' and x['name'] == name, self.getAll()))
12+
if len(ll):
13+
return ll[0]['projectId']
14+
else:
15+
raise ProjectNotFound("No project named '{}'.".format(name))
16+
17+
def getAll(self):
18+
""""Returns json representation of all projects."""
19+
return self.session.get('Project', **{'params': {'options': 'None'}}).json()

0 commit comments

Comments
 (0)