def fetch(self, client, method, url, data=None, headers=None, json_format=True): body = json.dumps(data) if json_format and data else data uri, headers, body = self.client.sign(url, http_method=method, body=data, headers=headers) resp = request(method, uri, headers=headers, data=body) return resp.content, resp.status_code
def clean(self): super(ContactForm, self).clean() base_url = settings.FWDFORM_API_URL.rstrip('/') fwdform_endpoint = '{base_url}/{site_token}/'.format( base_url=base_url, site_token=settings.FWDFORM_SITE_TOKEN ) payload = { 'name': 'Contact Form {id}'.format(id=self.pk or ''), 'recipients': self.email } if self.hashid: payload['hashid'] = self.hashid try: response = request('post', fwdform_endpoint, data=payload) response.raise_for_status() except RequestException as e: # pragma: no cover error = e.response.json() msg = _('FwdForm Error: {msg}').format( msg=error.get('message', _('Something\'s not right.')) ) raise ValidationError(msg) if response.status_code == HTTP_201_CREATED: data = response.json() self.hashid = data.get('hashid', '')
def _make_request(self, verb, base_url, params, headers): return api.request(verb, base_url, data=params if verb=='POST' else dict(), params=params if verb!='POST' else dict(), headers=headers, verify=util.get_cert(), prefetch=False, config={'danger_mode' : True})
def _make_request(self, verb, base_url, params, headers, timeout=None): headers = dict(headers) if headers else {} headers['User-Agent'] = 'CloudFormation Tools' return util.check_status(api.request(verb, base_url, **util.req_opts({ 'data' : params if verb=='POST' else dict(), 'params' : params if verb!='POST' else dict(), 'headers' : headers, 'proxies' : self._proxyinfo, 'timeout' : timeout })))
def make_request(self, method, endpoint, data=None, id=None): response = request(method, self.build_url(endpoint, id), json=data, headers={"Authentication": self.api_key, "Content-Type": "application/json" } ) if response.status_code in self.ERROR_CODES: raise GraphCommonsException(status_code=response.status_code, message=GraphCommons.get_error_message(response)) return response
def send(self, **kwargs): """Send a Request.""" try: resp = request(self.method, self.url, headers=self.headers, params=self.params, data=self.data, cookies=self.cookies, allow_redirects=self.allow_redirects, **kwargs) except requests.RequestException as ex: raise ex return Response(resp)
def fetch_gdata(resource, parts, identifier): access_token = settings.DJANGOCMS_YOUTUBE_API_KEY if access_token is None: msg = _("Missing DJANGOCMS_YOUTUBE_API_KEY settings") raise YoutubeAPIError(500, "ImproperlyConfigured", force_text(msg)) url_template = "{resource}?part={parts}&fields=items&maxResults=1&id={id}&key={access_token}" url = url_template.format(resource=resource, parts=",".join(parts), id=identifier, access_token=access_token) base_url = "https://www.googleapis.com/youtube/v3/" try: response = request("get", url="{base_url}{url}".format(base_url=base_url, url=url)) response.raise_for_status() except RequestException as e: error = e.response.json().get("error") raise YoutubeAPIError(error["code"], "YoutubeAPIError", error["message"]) else: items = response.json().get("items", []) if not items: raise YoutubeAPI404() return items[0]
def get_video_details(video_id): access_token = settings.DJANGOCMS_YOUTUBE_API_KEY if access_token is None: msg = _('Missing DJANGOCMS_YOUTUBE_API_KEY settings') raise YoutubeAPIError(500, 'ImproperlyConfigured', force_text(msg)) yt_endpoint = \ 'https://www.googleapis.com/youtube/v3/videos' \ '?part=snippet,player,status,contentDetails&id={0}&key={1}'.format( video_id, access_token) try: response = request('get', url=yt_endpoint) response.raise_for_status() except RequestException as e: error = e.response.json().get('error') raise YoutubeAPIError(error['code'], 'YoutubeAPIError', error['message']) else: results = response.json().get('items', []) if not results: msg = _('This video does not exist.') raise YoutubeAPIError(404, 'videoNotFound', force_text(msg)) else: return results[0]
def request(self, method, url, **kwargs): "Build remote url request." return request(method, url, **kwargs)
def make_request(method, url, headers=None, body=None): return request(method.lower(), url, headers=None, data=body)
#!/usr/bin/env python # -*- coding: utf-8 -*- from functools import partial from itertools import cycle, islice, izip from qc import integers, unicodes from requests.api import request import sys, urlparse, yaml MAX_NUM = 10 types = { 'str' : partial(unicodes, size=(0,10), minunicode=0, maxunicode=127), 'int' : partial(integers, low=0, high=100000), } with open(sys.argv[1]) as fn: data = yaml.load(fn.read()) base = data['base'] for view in data['views']: for parameters in zip(*[list(islice(izip(cycle([f['name']]), types[f['expected']]()), 0, MAX_NUM)) for f in view['fields']]): r = request(view['method'], urlparse.urljoin(base, view['url']), data=dict(parameters)) print r.url, r.status_code, dict(parameters), r.text
def request(self, method, url, **kwargs): "Build remote url request." kwargs['verify'] = self.verify_ssl return request(method, url, **kwargs)
order='name', limit=10000) so_results_df = pd.DataFrame.from_records(socrata_results) print(so_results_df.head(), "\n") print("There are " + str(so_results_df['name'].count()) + " health care provided whose credentials superseded in the last year.") newfile = so_results_df.to_json('Socrata_94to95_superseded_2021.json', orient='records', indent=2) #Pure soQL query print("\n__________________ALT QUERY 1 - SoQL_____________________\n") soql_results = request( "get", "https://data.wa.gov/resource/qxh8-f4bd.json?" + "$select=credentialnumber,firstname||' '||lastname as name,birthyear,credentialtype,status,firstissuedate &" "$where=birthyear::number between 1994 and 1995 and " + "status='SUPERSEDED' and " + "firstissuedate::number > 20210601 &" + "$limit=5000").json() soql_results_df = pd.DataFrame(soql_results) print(soql_results_df, "\n") print( "There are " + str(soql_results_df['name'].count()) + " health care provided whose credentials superseded since June of this year\n" ) newfile2 = soql_results_df.to_json('SoQL_94to95_superseded_since_June.json', orient='index', indent=2) #Beautiful Soup query
def request(self, url, method='post', **kwargs): headers = {'User-Agent': self.get_akismet_ua()} return request(method, url, headers=headers, **kwargs)