Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tailored user-agent in requests to API #12

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions annif_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"""Module for accessing Annif REST API"""

import requests
from importlib import metadata


# Default API base URL
API_BASE = 'https://api.annif.org/v1/'
Expand All @@ -12,24 +14,31 @@ class AnnifClient:

def __init__(self, api_base=API_BASE):
self.api_base = api_base
version = metadata.version("annif-client")
self._headers = {
"User-Agent": f"Annif-client/{version}",
}

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

@property
def projects(self):
"""Get a list of projects available on the API endpoint"""
req = requests.get(self.api_base + 'projects')
req = requests.get(self.api_base + 'projects', headers=self._headers)
req.raise_for_status()
return req.json()['projects']

def get_project(self, project_id):
"""Get a single project by project ID"""
req = requests.get(self.api_base + 'projects/{}'.format(project_id))
req = requests.get(
self.api_base + 'projects/{}'.format(project_id),
headers=self._headers
)
if req.status_code == 404:
raise ValueError(req.json()['detail'])
req.raise_for_status()
Expand All @@ -51,7 +60,7 @@ def suggest(self, project_id, text, limit=None, threshold=None):
payload['threshold'] = threshold

url = self.api_base + 'projects/{}/suggest'.format(project_id)
req = requests.post(url, data=payload)
req = requests.post(url, data=payload, headers=self._headers)
if req.status_code == 404:
raise ValueError(req.json()['detail'])
req.raise_for_status()
Expand All @@ -73,7 +82,8 @@ def suggest_batch(self, project_id, documents, limit=None, threshold=None):
params['threshold'] = threshold

url = self.api_base + 'projects/{}/suggest-batch'.format(project_id)
req = requests.post(url, json=payload, params=params)
req = requests.post(
url, json=payload, params=params, headers=self._headers)
if req.status_code == 404:
raise ValueError(req.json()['detail'])
req.raise_for_status()
Expand All @@ -83,7 +93,7 @@ def learn(self, project_id, documents):
"""Further train an existing project on a text with given subjects."""

url = self.api_base + 'projects/{}/learn'.format(project_id)
req = requests.post(url, json=documents)
req = requests.post(url, json=documents, headers=self._headers)
if req.status_code == 404:
raise ValueError(req.json()['detail'])
req.raise_for_status()
Expand Down
13 changes: 13 additions & 0 deletions tests/test_annif_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import os.path
import pytest
import responses
import requests
import importlib
import unittest


@pytest.fixture(scope='module')
Expand Down Expand Up @@ -42,3 +45,13 @@ def test_projects(client):
body=open(datafile).read())
result = client.projects
assert len(result) == 2


def test_headers(client):
with unittest.mock.patch("requests.get"):
client.api_info

version = importlib.metadata.version("annif-client")
assert requests.get.call_args.kwargs["headers"] == {
"User-Agent": f"Annif-client/{version}"
}