将请求响应json转换为Python类对象
嗨,我正在为pexels API写一个Python API包装器, 我的API响应如下:
{
"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"
}
我希望响应中的每件事都可以作为类对象访问,因此尝试制作单独的自定义对象,我的 type.py
但是我无法访问诸如photos 1 .alt
。功能:
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的searchResponse代码中,照片是字典列表,而不是照片实例列表。尝试使用列表comp来实例化多个照片实例,
然后您可以访问这样的实例的alt
the Off class定义的
唯一更改是self.photos
In your SearchResponse code, photos is a list of dictionary and not a list of Photo instance. Try using a list comp to instantiate multiple Photo instance
you can then access the alt of an instance like this
The full class definition
The only change is self.photos