def request(self, url, req_type, json=None, data=None, params=None): """ Base request using requests libary. `Args:` url: str The url request string req_type: str The request type. One of GET, POST, PATCH, DELETE, OPTIONS json: dict The payload of the request object. By using json, it will automatically serialize the dictionary data: str or byte or dict The payload of the request object. Use instead of json in some instances. params: dict The parameters to append to the url (e.g. http://myapi.com/things?id=1) raise_on_error: If the request yields an error status code (anything above 400), raise an error. In most cases, this should be True, however in some cases, if you are looping through data, you might want to ignore individual failures. `Returns:` requests response """ return _request(req_type, url, headers=self.headers, auth=self.auth, json=json, data=data, params=params)
def request(self, url, req_type='GET', post_data=None, args=None, auth=False): # Internal request method if auth: if not self.api_key: raise TypeError('This method requires an api key.') else: header = {'Authorization': 'Bearer ' + self.api_key} else: header = None r = _request(req_type, url, json=post_data, params=args, headers=header) if 'error' in r.json(): raise ValueError('API Error:' + str(r.json()['error'])) return r
def send(self, mtd='GET', *args, **kwargs): try: return _request(method=mtd, headers={ 'User-Agent': useragent(), 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive' }, timeout=self.timeout, cookies=self.cookie, proxies=self.proxy, *args, **kwargs) except ProxyError: raise RequestException('Error Connect To Proxies') except (ConnectTimeout, ReadTimeout): raise RequestException('Connection Timeout To Server') except NewConnectionError: raise RequestException("Address Can't be Resolved") except ConnectionError: raise RequestException( 'Error Connecting To Host Check Your Internet Connection') except TooManyRedirects: raise RequestException('Too Many Redirects Error') except UnicodeDecodeError: pass
def request(method, url, **kwargs): """Make http request""" try: response = _request(method, url, **kwargs) response.status = response.status_code except Exception: response = to_object({}) response.status = 500 # Internal server error return response
def request(**request_kw): request_kw.setdefault('timeout', 5) try: response = _request(**request_kw) error = None if response: response = handle_response(response) except ConnectionError: response = None error = None print("Connecting...") except Exception as exc: response = None error = exc print("An exception occurred: ", repr(error)) return response
def request(method: str, url: str, timeout: int = 5, **kwargs) -> Response: '''Just wrapping request in exception handling''' try: response = _request(method, url, headers=HEADERS, timeout=timeout, **kwargs) except (ConnectionError, Timeout): raise BenwaOnlineRequestError( title='Connection timed out.', detail='Unable to connect to API service.') try: response.raise_for_status() except HTTPError: error = handle_http_error(response) raise BenwaOnlineRequestError(error) return response
def get(url, output=".", extract=False): """ Get a file or archive from an URL. Args: output (str): Output file or directory path. url (str): Input URL. extract (bool): If True, extract archive. """ response = _request("GET", url, stream=True) response.raise_for_status() if extract: with _tarfile_open(fileobj=response.raw) as archive: archive.extractall(output) else: if not _isfile(output): output = _join(output, _basename(_urlparse(url).path)) with open(output, "wb") as dest: for chunk in response.iter_content(): dest.write(chunk)
def validate_token(request): BASE_URL = 'https://login.microsoftonline.com/{tenant_id}'.format( tenant_id=settings.TENANT_ID) JWKS_URL = '{base_url}/discovery/v2.0/keys?p={policy}'.format( base_url=BASE_URL, policy=settings.POLICY) if request.GET.get('error'): if 'The user has cancelled entering self-asserted information.' in request.GET.get( 'error_description'): return HttpResponseRedirect('/azure_auth/login/') else: return HttpResponse('failure, ' + str(reques.GET.get('error_description'))) else: id_token = request.GET['id_token'] jwt_header_json = base64url_decode(id_token.split('.')[0]) jwt_header = json.loads(jwt_header_json.decode('ascii')) resp = _request(url=JWKS_URL, method="GET") pub_key_val = '' for key in resp.json()['keys']: if key['kid'] == jwt_header['kid']: pub_key = RSAAlgorithm.from_jwk(json.dumps(key)) pub_key_val = pub_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo) try: user_info = jwt_decode(id_token, key=pub_key_val, algorithms=jwt_header['alg'], audience=settings.CLIENT_ID, leeway=0) return HttpResponse('Login success, ' + json.dumps(user_info)) except (DecodeError, ExpiredSignature) as error: return HttpResponse('failure, ' + str(error)) except Exception as e: return HttpResponse('failure, ' + str(e))
def request(url='', method='get', params={}, data={}, json={}, headers={}, cookies=None, files=None, auth=None, timeout=None, verify=True, proxies='socks5://127.0.0.1:9050', follow_redirects=True, local=False, max_retries=5): """ Request with proxies, `socks5://localhost:9050` by default and return the response (requests.model.Response) NOTE: Defaults to `get` http method, and if data/json/files are passed then defaults to `post` NOTE: Auto adds scheme `http` (insecure) if no scheme passed Parameters: url (str): URL to request (eg.: 'https://httpbin.org') method (str): HTTP Method to use (eg.: 'post') params (dict): URL parameters to use (eg.: {'q': 'test'} == `https://httpbin.org/get/?q=test`) data (dict): Request body data to use (eg.: {'id': 99}) json (dict): Request JSON body to use (eg.: {'user': '******', 'pass': '******'ll bypass proxies in use max_retries (int): Maximum number of retries to be made on errors Returns: response (requests.model.Response): HTTP Response of the specific class "mentioned" FROM the request made. """ global RETRIES # print(url, method, params, data, json, headers, cookies, files, auth, timeout, proxies, local, max_retries) if not url: return None if proxies and type(proxies) == dict: PROXIES = proxies if type(proxies) == str: PROXIES = {'http': proxies, 'https': proxies} scheme = url.split('://')[0] if len(url.split('://')) == 1: scheme = 'http' url = f'{scheme}://{url}' if scheme not in ('http', 'https'): raise TypeError(bad(f'{scheme} -> does not look like HTTP(S) url')) if data or json or files: method = 'post' if headers: if not 'User-Agent' in headers: headers['User-Agent'] = f'dore/{__version__}' else: headers = {'User-Agent': f'dore/{__version__}'} if not verify: # print(warn('Insecure requests (NO TLS/SSL CERT VERIFICATION) -> Enabled')) _urllib3.disable_warnings(_urllib3.exceptions.InsecureRequestWarning) if local: # print(warn('Local mode enabled (NO PROXIES BEING USED) -> `renew()` will not work')) PROXIES = {} if scheme == 'http': pass # print(warn('Not a TLS request -> Using an insecure protocol')) request_hash = sha512(bytify(locals())).hexdigest() if request_hash not in RETRIES: RETRIES[request_hash] = 0 try: RETRIES[request_hash] += 1 if max_retries >= RETRIES[request_hash]: response = _request(method, url, params=params, data=data, json=json, headers=headers, cookies=cookies, files=files, auth=auth, timeout=timeout, proxies=PROXIES, verify=verify, allow_redirects=follow_redirects) del RETRIES[request_hash] return response except Exception as err: if proxy_works(PROXIES): print(bad(f'Error [{RETRIES[request_hash]}] -> {err}')) response = request(url, method, params, data, json, headers, cookies, files, auth, timeout, verify, proxies, follow_redirects, local, max_retries) try: del RETRIES[request_hash] except KeyError: # I don't know what's wrong here :( pass return response else: if PROXIES: print(bad('Bruh! -> Check if the proxy is working properly..'))
def request(method, url, **kwargs): return _request(method, url, **_add_request_id(kwargs))