Skip to content

Commit

Permalink
Add method for fetching info of API and demo it
Browse files Browse the repository at this point in the history
  • Loading branch information
juhoinkinen committed Aug 23, 2023
1 parent 9b35db9 commit e97d866
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion annif_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class AnnifClient:
def __init__(self, api_base=API_BASE):
self.api_base = api_base

@property
def api_info(self):
"""Get basic information of the API endpoint"""
req = requests.get(self.api_base)
req.raise_for_status()
return req.json()

@property
def projects(self):
"""Get a list of projects available on the API endpoint"""
Expand Down Expand Up @@ -74,7 +81,8 @@ def __str__(self):

print("* Creating an AnnifClient object")
annif = AnnifClient()
print("Now we have an AnnifClient object:", annif)
print(f"* The client uses Annif API at {annif.api_base}")
print(f"* The version of Annif serving the API is {annif.api_info['version']}")
print()
print("* Finding the available projects")
for project in annif.projects:
Expand Down
4 changes: 4 additions & 0 deletions tests/data/api-info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "Annif REST API",
"version": "0.12.3"
}
11 changes: 11 additions & 0 deletions tests/test_annif_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ def test_create_client_api_base():
assert client.api_base == 'http://localhost:5000/v1/'


@responses.activate
def test_api_info(client):
datafile = os.path.join(os.path.dirname(__file__), 'data/api-info.json')
responses.add(responses.GET,
'https://api.annif.org/v1/',
body=open(datafile).read())
result = client.api_info
assert result['title'] == 'Annif REST API'
assert result['version'] == '0.12.3'


@responses.activate
def test_projects(client):
datafile = os.path.join(os.path.dirname(__file__), 'data/projects.json')
Expand Down

0 comments on commit e97d866

Please sign in to comment.