Submit the HTTP request with the running session or a new session. Returns: A dictionary of the response data.
(
*,
current_session: Optional[ClientSession],
timeout: int,
logger: Logger,
http_verb: str,
api_url: str,
req_args: dict,
)
| 154 | |
| 155 | |
| 156 | async def _request_with_session( |
| 157 | *, |
| 158 | current_session: Optional[ClientSession], |
| 159 | timeout: int, |
| 160 | logger: Logger, |
| 161 | http_verb: str, |
| 162 | api_url: str, |
| 163 | req_args: dict, |
| 164 | ) -> Dict[str, any]: |
| 165 | """Submit the HTTP request with the running session or a new session. |
| 166 | Returns: |
| 167 | A dictionary of the response data. |
| 168 | """ |
| 169 | session = None |
| 170 | use_running_session = current_session and not current_session.closed |
| 171 | if use_running_session: |
| 172 | session = current_session |
| 173 | else: |
| 174 | session = aiohttp.ClientSession( |
| 175 | timeout=aiohttp.ClientTimeout(total=timeout), |
| 176 | auth=req_args.pop("auth", None), |
| 177 | ) |
| 178 | |
| 179 | response = None |
| 180 | try: |
| 181 | async with session.request(http_verb, api_url, **req_args) as res: |
| 182 | data = {} |
| 183 | try: |
| 184 | data = await res.json() |
| 185 | except aiohttp.ContentTypeError: |
| 186 | logger.debug(f"No response data returned from the following API call: {api_url}.") |
| 187 | except json.decoder.JSONDecodeError as e: |
| 188 | message = f"Failed to parse the response body: {str(e)}" |
| 189 | raise SlackApiError(message, res) |
| 190 | |
| 191 | response = { |
| 192 | "data": data, |
| 193 | "headers": res.headers, |
| 194 | "status_code": res.status, |
| 195 | } |
| 196 | finally: |
| 197 | if not use_running_session: |
| 198 | await session.close() |
| 199 | return response |
no test coverage detected