Skip to content

Commit 69c2b23

Browse files
Merge pull request #26 from mishaschwartz/0.2.0
Rewrite everything with the requests library
2 parents 206116d + 6b44dfd commit 69c2b23

File tree

7 files changed

+920
-723
lines changed

7 files changed

+920
-723
lines changed

.travis.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ python:
33
- "3.6"
44
- "3.7"
55
- "3.8"
6-
# command to install dependencies
7-
install:
8-
- pip install pytest
9-
- pip install hypothesis
106
# command to run tests
117
script:
12-
- pytest
8+
- python setup.py test

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
##[unreleased]
33
- Added new methods for adding and removing extra marks (#15)
44
- Fixed bug which caused file uploads to be PUT requests by default instead of POST (#22)
5+
- Rewrite everything to use the requests library (#26)
56

67
##[v0.1.0]
78
- this release includes functions to call all API routes for MarkUs version 1.9.0 (#10, #12, #13, #14, #18)

markusapi/markusapi.py

Lines changed: 193 additions & 254 deletions
Large diffs are not rendered by default.

markusapi/response_parser.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from functools import wraps
2+
from typing import List, Union, Dict, Callable
3+
4+
5+
def parse_response(expected: str) -> Callable:
6+
"""
7+
Decorator for a function that returns a requests.Response object.
8+
This decorator parses that response depending on the value of <expected>.
9+
10+
If the response indicates the request failed (status >= 400) a dictionary
11+
containing the response status and message will be returned. Otherwise,
12+
the content will be parsed and a dictionary or list will be returned if
13+
expected == 'json', a string will be returned if expected == 'text' and
14+
a binary string will be returned if expected == 'content'.
15+
16+
This also updates the return annotation for the wrapped function according
17+
to the expected return value type.
18+
"""
19+
20+
def _parser(f):
21+
@wraps(f)
22+
def _f(*args, **kwargs):
23+
response = f(*args, **kwargs)
24+
if not response.ok or expected == "json":
25+
return response.json()
26+
if expected == "content":
27+
return response.content
28+
if expected == "text":
29+
return response.text
30+
return response.json()
31+
32+
f.__annotations__["return"] = _get_expected_return(expected)
33+
return _f
34+
35+
return _parser
36+
37+
38+
def _get_expected_return(expected: str) -> type:
39+
if expected == "json":
40+
return Union[Dict[str, str], List[Dict[str, str]]]
41+
elif expected == "content":
42+
return Union[Dict[str, str], bytes]
43+
elif expected == "text":
44+
return Union[Dict[str, str], bytes]
45+
return Dict[str, str]

0 commit comments

Comments
 (0)