Hi i'm writing a python API wrapper for Pexels API, I have API response as follow:
{
"total_results": 10000,
"page": 1,
"per_page": 1,
"photos": [
{
"id": 3573351,
"width": 3066,
"height": 3968,
"url": "https://www.pexels.com/photo/trees-during-day-3573351/",
"photographer": "Lukas Rodriguez",
"photographer_url": "https://www.pexels.com/@lukas-rodriguez-1845331",
"photographer_id": 1845331,
"avg_color": "#374824",
"src": {
"original": "https://images.pexels.com/photos/3573351/pexels-photo-3573351.png",
"large2x": "https://images.pexels.com/photos/3573351/pexels-photo-3573351.png?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"large": "https://images.pexels.com/photos/3573351/pexels-photo-3573351.png?auto=compress&cs=tinysrgb&h=650&w=940",
"medium": "https://images.pexels.com/photos/3573351/pexels-photo-3573351.png?auto=compress&cs=tinysrgb&h=350",
"small": "https://images.pexels.com/photos/3573351/pexels-photo-3573351.png?auto=compress&cs=tinysrgb&h=130",
"portrait": "https://images.pexels.com/photos/3573351/pexels-photo-3573351.png?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800",
"landscape": "https://images.pexels.com/photos/3573351/pexels-photo-3573351.png?auto=compress&cs=tinysrgb&fit=crop&h=627&w=1200",
"tiny": "https://images.pexels.com/photos/3573351/pexels-photo-3573351.png?auto=compress&cs=tinysrgb&dpr=1&fit=crop&h=200&w=280"
},
"liked": false,
"alt": "Brown Rocks During Golden Hour"
}
],
"next_page": "https://api.pexels.com/v1/search/?page=2&per_page=1&query=nature"
}
I wanted each of the things in the response to be accessed as class object so tried to make separate custom object, my types.py
but i can't access objects like photos1.alt
my client function:
def _make_request(
self,
path: str,
method: str = "get",
**kwargs: Dict[Any, Any]
) -> Tuple[Union[Dict, str], requests.Response]:
header = {'Authorization': self._token}
req = self.session.request(method, f'{self._host}/{path}', headers=header,**kwargs)
if req.status_code in [200, 201]:
try:
return req.json(), req
except JSONDecodeError:
return req.text, req
elif req.status_code == 400:
raise PexelsError("Bad Request Caught")
else:
raise PexelsError(f"{req.status_code} : {req.reason}")
def search_photos(
self,
query: str,
orientation: str = "",
size:str = "",
color: str = "",
locale: str = "",
page: int = 1,
per_page: int = 15,
**kwargs
) -> SearchResponse:
data, req = self._make_request(f"search?query={query}")
return SearchResponse(**data)