Coverage for src/retrospeedlab/maintainer/articles/api.py: 67%

30 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-12-02 22:44 +0000

1from typing import Any 

2 

3import httpx 

4 

5from retrospeedlab.addresses import API_URL 

6 

7ARTICLES = "/articles" 

8LINKEDIN = "/articles/linkedin" 

9CONNECT_URL = f"{API_URL}/articles/linkedin/connect" 

10TIMEOUT_SECONDS = 60.0 

11REFUSED = 422 

12 

13 

14class ArticleApi: 

15 def __init__(self, api: str, key: str) -> None: 

16 self.api = api 

17 self.headers = {"X-API-Key": key} 

18 

19 def client(self) -> httpx.Client: 

20 return httpx.Client(base_url=self.api, headers=self.headers, timeout=TIMEOUT_SECONDS) 

21 

22 def upload(self, slug: str, body: dict[str, Any]) -> None: 

23 with self.client() as backend: 

24 response = backend.put(f"{ARTICLES}/{slug}", json=body) 

25 if response.status_code == REFUSED: 

26 raise ValueError(f"The backend refused draft {slug}: {response.text}") 

27 response.raise_for_status() 

28 

29 def state(self, slug: str) -> dict[str, Any]: 

30 with self.client() as backend: 

31 return backend.get(f"{ARTICLES}/{slug}").raise_for_status().json() 

32 

33 def titles(self) -> list[str]: 

34 with self.client() as backend: 

35 listed = backend.get(ARTICLES).raise_for_status().json() 

36 return [article["title"] for article in listed] 

37 

38 def linkedin(self) -> dict[str, Any]: 

39 with self.client() as backend: 

40 return backend.get(LINKEDIN).raise_for_status().json()