refactor: restructure api module and implement articles pagination with dynamic routing
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import requests
|
||||
|
||||
API_BASE_URL = 'http://localhost:8000/api'
|
||||
|
||||
def list_articles(page=1, search=None, token=None):
|
||||
params = {'page' : page}
|
||||
|
||||
if search:
|
||||
params['search'] = search
|
||||
|
||||
headers = {}
|
||||
if token:
|
||||
headers['Authorization'] = f"Bearer {token}"
|
||||
|
||||
r = requests.get(
|
||||
f"{API_BASE_URL}/articles",
|
||||
params=params,
|
||||
headers=headers,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
def get_article(article_id, token=None):
|
||||
headers={}
|
||||
|
||||
if token:
|
||||
headers['Authorization'] = f"Bearer {token}"
|
||||
|
||||
r = requests.get(
|
||||
f"{API_BASE_URL}/articles/{article_id}",
|
||||
headers = headers,
|
||||
timeout = 10
|
||||
)
|
||||
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
@@ -0,0 +1,23 @@
|
||||
import requests
|
||||
|
||||
API_BASE_URL = "http://127.0.0.1:8000"
|
||||
|
||||
def login(username: str, password: str):
|
||||
url = f"{API_BASE_URL}/api/auth/login/"
|
||||
|
||||
response = requests.post(url, json={
|
||||
"email": username,
|
||||
"password": password
|
||||
})
|
||||
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def get_me(token: str):
|
||||
url = f"{API_BASE_URL}/api/auth/me/"
|
||||
response = requests.get(
|
||||
url,
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
Reference in New Issue
Block a user