12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- """Github utils get HTTP response."""
- import copy
- import json
- from typing import Dict, List, Optional
- import requests
- class RespGet:
- """Get response from GitHub restful API.
- :param url: URL to requests GET method.
- :param headers: headers for HTTP requests.
- :param param: param for HTTP requests.
- """
- def __init__(self, url: str, headers: dict, param: Optional[dict] = None):
- self.url = url
- self.headers = headers
- self.param = param
- @staticmethod
- def get(url: str, headers: dict, params: Optional[dict] = None) -> Dict:
- """Get single response dict from HTTP requests by given condition."""
- resp = requests.get(url=url, headers=headers, params=params)
- if not resp.ok:
- raise ValueError("Requests error with", resp.reason)
- return json.loads(resp.content)
- def get_single(self) -> Dict:
- """Get single response dict from HTTP requests by given condition."""
- return self.get(url=self.url, headers=self.headers, params=self.param)
- def get_total(self) -> List[Dict]:
- """Get all response dict from HTTP requests by given condition.
- Will change page number until no data return.
- """
- total = []
- curr_param = copy.deepcopy(self.param)
- while True:
- curr_param["page"] = curr_param.setdefault("page", 0) + 1
- content_dict = self.get(
- url=self.url, headers=self.headers, params=curr_param
- )
- data = content_dict.get("items")
- if not data:
- return total
- total.extend(data)
|