Skip to content

Commit 866eab2

Browse files
committed
Dynamically load API version
1 parent 7d8037c commit 866eab2

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ Python wrapper around SCUTTLE API.
55

66
## Usage
77

8+
Create a wrapper for a given wiki and API version:
9+
10+
```python
11+
from scuttle import scuttle
12+
13+
wiki = scuttle('en', 1)
14+
```
15+
816
## Testing
917

1018
Install [Pipenv](https://pypi.org/project/pipenv/) and install development

scuttle/versions/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#!/usr/bin/env python3
22

33
"""Provides generic methods for accessing version 1 of the API."""
4+
5+
class Api:
6+
pass

scuttle/wrapper.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,32 @@
33
"""Provides methods to interface with a given API instance."""
44

55
import requests
6+
import importlib
67

7-
def scuttle(wiki, api_version=None):
8+
print(importlib.import_module('.v1', 'scuttle.versions'))
9+
10+
def scuttle(wiki, api_key, api_version=1):
811
"""Create a new API wrapper for a given wiki and API version.
912
1013
str `wiki`: The domain of the wiki.
14+
str `api_key`: Your SCUTTLE API key.
1115
int `api_version`: The version of the API to use. If not provided, defaults
12-
to latest.
16+
to 1.
1317
"""
18+
if api_version is None:
19+
raise NotImplementedError
20+
return ApiWrapper(wiki, api_key, api_version)
21+
22+
class ApiWrapper:
23+
def __init__(self, wiki, api_key, api_version):
24+
self.domain = wiki
25+
self.api_key = api_key
26+
self.version = api_version
1427

28+
try:
29+
api_module = importlib.import_module(".v{}".format(self.version),
30+
'scuttle.versions')
31+
except ModuleNotFoundError:
32+
raise ModuleNotFoundError("API version {} does not exist."
33+
.format(self.version))
34+
self.api = api_module.Api()

test/test_api.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,24 @@
44
import pytest
55
import scuttle
66

7-
TOKEN = os.environ['SCUTTLE_API_KEY']
7+
API_KEY = os.environ['SCUTTLE_API_KEY']
88

9-
def test_something():
10-
wiki = scuttle.scuttle('en', 1)
9+
def test_basic():
10+
wiki = scuttle.scuttle('en', None, 1)
11+
assert wiki.domain == 'en'
12+
assert wiki.version == 1
13+
assert isinstance(wiki.api, scuttle.versions.v1.Api)
14+
15+
def test_get_nonexistent_version():
16+
with pytest.raises(ModuleNotFoundError) as e:
17+
wiki = scuttle.scuttle('en', None, 0)
18+
assert str(e.value) == "API version 0 does not exist."
19+
20+
def test_get_latest_version():
21+
wiki = scuttle.scuttle('en', None)
22+
assert wiki.version == 1
23+
24+
def test_get_wikis():
25+
wiki = scuttle.scuttle('en', API_KEY, 1)
26+
print(wiki.wikis())
27+
assert False

0 commit comments

Comments
 (0)