Arya’s API Framework¶
This module is designed to be a base for the creation of clients that interact with RESTful web APIs.
- Features:
Disclaimer
This library is not intended for fast, performant code. It is instead optimized for user-friendliness and strict data typing. While this will likely not be a noticeable issue for many users, please keep this in mind.
Installation¶
To install the sync branch:
pip install arya-api-framework[sync]
To install the async branch:
pip install arya-api-framework[async]
Getting Started¶
This example is a minimal example. However, it is still much more than most libraries have for a getting started page. This is because of this modules usage of Pydantic for data validation from API responses. If you would like a breakdown of this, please see the Breakdown section, or take a look at the available Guides.
1from arya_api_framework import BaseModel, Response
2
3# Data models
4class Geo(BaseModel):
5 lat: float
6 lng: float
7
8class Address(BaseModel):
9 street: str
10 suite: str
11 city: str
12 zipcode: str
13 geo: Geo
14
15class Company(BaseModel):
16 name: str
17 catchPhrase: str
18 bs: str
19
20class User(Response):
21 id: int
22 name: str
23 email: str
24 address: Address
25 phone: str
26 website: str
27 company: Company
28
29# Query models
30class AddressQuery(BaseModel):
31 city: Optional[str]
32
33class UserQuery(BaseModel):
34 username: Optional[str]
35 address: Optional[AddressQuery]
1from arya_api_framework import SyncClient
2from pydantic import validate_arguments
3
4from models import User, UserQuery, AddressQuery
5
6class PlaceholderClient(SyncClient, uri="https://jsonplaceholder.typicode.com"):
7 def get_users(self):
8 # https://jsonplaceholder.typicode.com/users
9 return self.get('/users', response_format=User)
10
11 @validate_arguments()
12 def get_user_by_id(self, id: int):
13 # https://jsonplaceholder.typicode.com/users/<id>
14 return self.get(f'/users/{id}', response_format=User)
15
16 @validate_arguments()
17 def search_user_by_username(self, name: str):
18 # https://jsonplaceholder.typicode.com/users?username=<name>
19 query = UserQuery(username=name)
20
21 return self.get('/users', parameters=query, response_format=User)
22
23 @validate_arguments()
24 def search_user_by_city(self, city: str):
25 # https://jsonplaceholder.typicode.com/users?address.city=<city>
26 query = UserQuery(address=AddressQuery(city=city))
27
28 return self.get('/users', parameters=query, response_format=User)
29
30 @validate_arguments()
31 def search_user_by_username_and_city(self, name: str, city: str):
32 # https://jsonplaceholder.typicode.com/users?username=<name>&address.city=<city>
33 query = UserQuery(username=name, address=AddressQuery(city=city))
34
35 return self.get('/users', parameters=query, response_format=User)
1from api import PlaceholderClient
2
3if __name__ == "__main__":
4 client = PlaceholderClient()
5
6 users = client.get_users()
7 print(users)
8
9 user = client.get_user_by_id(3)
10 print(user)
11
12 lookup = client.search_user_by_username("Bret")
13 print(lookup)
14
15 lookup = client.search_user_by_city("Gwenborough")
16 print(lookup)
17
18 lookup = client.search_user_by_username_and_city("Bret", "Gwenborough")
19 print(lookup)
Guides¶
These guides are intended to be a place where those looking to really take advantage of the features this system has can get started.
API Reference¶
Information¶
Here lies a bunch of random information related to the project at least a little bit.
Glossary¶
If you are struggling to find specific information, see these references:
TODO Features¶
- Rate Limits (Allow rate limit application for limited APIs.)
- Response Caching (Reduce processing times/network load.)
- Sub-Clients (For creating API category modules.)