def send_file(self, chat_id, file_id=None, file=None, caption=None, reply_msg_id=None, forward_chat_id=None, forward_msg_id=None, inline_keyboard_markup=None, file_name=None): request = Request(method="GET", url="{}/messages/sendFile".format(self.api_base_url), params={ "token": self.token, "chatId": chat_id, "fileId": file_id, "caption": caption, "replyMsgId": reply_msg_id, "forwardChatId": forward_chat_id, "forwardMsgId": forward_msg_id, "inlineKeyboardMarkup": inline_keyboard_markup }) if file: request.method = "POST" if file_name: request.files = {"file": (file_name, file)} else: request.files = {"file": file} return self.http_session.send(request.prepare(), timeout=self.timeout_s)
def _prepare_request(reddit_session, url, params, data, auth, files, method=None): """Return a requests Request object that can be "prepared".""" # Requests using OAuth for authorization must switch to using the oauth # domain. if getattr(reddit_session, '_use_oauth', False): headers = {'Authorization': 'bearer %s' % reddit_session.access_token} config = reddit_session.config for prefix in (config.api_url, config.permalink_url): if url.startswith(prefix): if config.log_requests >= 1: sys.stderr.write('substituting {} for {} in url\n'.format( config.oauth_url, prefix)) url = config.oauth_url + url[len(prefix):] break else: headers = {} headers.update(reddit_session.http.headers) if method: pass elif data or files: method = 'POST' else: method = 'GET' # Log the request if logging is enabled if reddit_session.config.log_requests >= 1: sys.stderr.write('{0}: {1}\n'.format(method, url)) if reddit_session.config.log_requests >= 2: if params: sys.stderr.write('params: {0}\n'.format(params)) if data: sys.stderr.write('data: {0}\n'.format(data)) if auth: sys.stderr.write('auth: {0}\n'.format(auth)) # Prepare request request = Request(method=method, url=url, headers=headers, params=params, auth=auth, cookies=reddit_session.http.cookies) if method == 'GET': return request # Most POST requests require adding `api_type` and `uh` to the data. if data is True: data = {} if not auth: data.setdefault('api_type', 'json') if reddit_session.modhash: data.setdefault('uh', reddit_session.modhash) request.data = data request.files = files return request
def _prepare_request(reddit_session, url, params, data, auth, files, method=None): """Return a requests Request object that can be "prepared".""" # Requests using OAuth for authorization must switch to using the oauth # domain. if getattr(reddit_session, '_use_oauth', False): bearer = 'bearer {0}'.format(reddit_session.access_token) headers = {'Authorization': bearer} config = reddit_session.config for prefix in (config.api_url, config.permalink_url): if url.startswith(prefix): if config.log_requests >= 1: msg = 'substituting {0} for {1} in url\n'.format( config.oauth_url, prefix) sys.stderr.write(msg) url = config.oauth_url + url[len(prefix):] break else: headers = {} headers.update(reddit_session.http.headers) if method: pass elif data or files: method = 'POST' else: method = 'GET' # Log the request if logging is enabled if reddit_session.config.log_requests >= 1: sys.stderr.write('{0}: {1}\n'.format(method, url)) if reddit_session.config.log_requests >= 2: if params: sys.stderr.write('params: {0}\n'.format(params)) if data: sys.stderr.write('data: {0}\n'.format(data)) if auth: sys.stderr.write('auth: {0}\n'.format(auth)) # Prepare request request = Request(method=method, url=url, headers=headers, params=params, auth=auth, cookies=reddit_session.http.cookies) if method == 'GET': return request # Most POST requests require adding `api_type` and `uh` to the data. if data is True: data = {} if isinstance(data, dict): if not auth: data.setdefault('api_type', 'json') if reddit_session.modhash: data.setdefault('uh', reddit_session.modhash) else: request.headers.setdefault('Content-Type', 'application/json') request.data = data request.files = files return request
def _prepare_request(reddit_session, url, params, data, auth, files, method=None): """Return a requests Request object that can be "prepared".""" # Requests using OAuth for authorization must switch to using the oauth # domain. if getattr(reddit_session, "_use_oauth", False): headers = {"Authorization": "bearer %s" % reddit_session.access_token} config = reddit_session.config for prefix in (config.api_url, config.permalink_url): if url.startswith(prefix): if config.log_requests >= 1: sys.stderr.write("substituting {} for {} in url\n".format(config.oauth_url, prefix)) url = config.oauth_url + url[len(prefix) :] break else: headers = {} headers.update(reddit_session.http.headers) if method: pass elif data or files: method = "POST" else: method = "GET" # Log the request if logging is enabled if reddit_session.config.log_requests >= 1: sys.stderr.write("{0}: {1}\n".format(method, url)) if reddit_session.config.log_requests >= 2: if params: sys.stderr.write("params: {0}\n".format(params)) if data: sys.stderr.write("data: {0}\n".format(data)) if auth: sys.stderr.write("auth: {0}\n".format(auth)) # Prepare request request = Request( method=method, url=url, headers=headers, params=params, auth=auth, cookies=reddit_session.http.cookies ) if method == "GET": return request # Most POST requests require adding `api_type` and `uh` to the data. if data is True: data = {} if not auth: data.setdefault("api_type", "json") if reddit_session.modhash: data.setdefault("uh", reddit_session.modhash) request.data = data request.files = files return request
def _prepare_request(lex_session, url, params, data, auth, files): """Return a requests Request object that can be "prepared".""" headers = {} headers.update(lex_session.http.headers) request = Request(method='GET', url=url, headers=headers, params=params, auth=auth, cookies=lex_session.http.cookies) if not data and not files: # GET request return request if not auth: data.setdefault('api_type', 'json') request.method = 'POST' request.data = data request.files = files return request
def _prepare_request(reddit_session, url, params, data, auth, files): """Return a requests Request object that can be "prepared".""" # Requests using OAuth for authorization must switch to using the oauth # domain. if getattr(reddit_session, '_use_oauth', False): headers = {'Authorization': 'bearer %s' % reddit_session.access_token} config = reddit_session.config # pylint: disable-msg=W0212 for prefix in (config._site_url, config._ssl_url): if url.startswith(prefix): if config.log_requests >= 1: sys.stderr.write('substituting %s for %s in url\n' % (config._oauth_url, prefix)) url = config._oauth_url + url[len(prefix):] break else: headers = {} headers.update(reddit_session.http.headers) # Log the request if logging is enabled if reddit_session.config.log_requests >= 1: sys.stderr.write('retrieving: %s\n' % url) if reddit_session.config.log_requests >= 2: sys.stderr.write('params: %s\n' % (params or 'None')) sys.stderr.write('data: %s\n' % (data or 'None')) if auth: sys.stderr.write('auth: %s\n' % str(auth)) # Prepare request request = Request(method='GET', url=url, headers=headers, params=params, auth=auth, cookies=reddit_session.http.cookies) if not data and not files: # GET request return request # Most POST requests require adding `api_type` and `uh` to the data. if data is True: data = {} if not auth: data.setdefault('api_type', 'json') if reddit_session.modhash: data.setdefault('uh', reddit_session.modhash) request.method = 'POST' request.data = data request.files = files return request
def send_file(self, chat_id, file_id=None, file=None, caption=None, reply_msg_id=None, forward_chat_id=None, forward_msg_id=None, inline_keyboard_markup=None, parse_mode=None, format_=None): if parse_mode and format_: raise Exception( "Cannot use format and parseMode fields at one time") if parse_mode: ParseMode(parse_mode) request = Request(method="GET", url="{}/messages/sendFile".format(self.api_base_url), params={ "token": self.token, "chatId": chat_id, "fileId": file_id, "caption": caption, "replyMsgId": reply_msg_id, "forwardChatId": forward_chat_id, "forwardMsgId": forward_msg_id, "inlineKeyboardMarkup": keyboard_to_json(inline_keyboard_markup), "parseMode": parse_mode, "format": format_to_json(format_) }) if file: request.method = "POST" request.files = {"file": file} return self.http_session.send(request.prepare(), timeout=self.timeout_s)
def _prepare_request(session, url, params, data, auth, files, method=None): """Return a requests Request object that can be "prepared".""" headers = {} headers.update(session.http.headers) if method: pass elif data or files: method = 'POST' else: method = 'GET' # Log the request if logging is enabled if session.config.log_requests >= 1: sys.stderr.write('{0}: {1}\n'.format(method, url)) if session.config.log_requests >= 2: if params: sys.stderr.write('params: {0}\n'.format(params)) if data: sys.stderr.write('data: {0}\n'.format(data)) if auth: sys.stderr.write('auth: {0}\n'.format(auth)) # Prepare request request = Request(method=method, url=url, headers=headers, params=params, auth=auth, cookies=session.http.cookies) if method == 'GET': return request # Most POST requests require adding `api_type` and `uh` to the data. if data is True: data = {} if isinstance(data, dict): if not auth: data.setdefault('api_type', 'json') if session.modhash: data.setdefault('uh', session.modhash) else: request.headers.setdefault('Content-Type', 'application/json') request.data = data request.files = files return request
def process_request(req): # api_stream = open(file, 'r') # api = yaml.load_all(api_stream) # for reqs in api: # for req in reqs: preq = Request() pr("Testing: {0}, which is a {1} request".format(req['request']['name'], req['request']['method'])) static_headers = process_static_headers(req['request']['headers']) var_headers = process_var_headers(req['request']['headers']) all_headers = dict(static_headers.items() + var_headers.items()) #print all_headers if req['request']['params']: preq.params = req['request']['params'] preq.headers = all_headers preq.method = req['request']['method'] preq.url = req['request']['uri'] if 'body' in req['request'].keys(): if req['request']['body']['type'] == 'post-vars': preq.data = req['request']['body']['value'] elif req['request']['body']['type'] == 'json': preq.json = req['request']['body']['value'] elif req['request']['body']['type'] == 'file': filepath = req['request']['body']['value']['filepath'] preq.files = {filepath: open(filepath, 'rb')} else: raise Exception('Payload type unsupported') sys.exit(1) rsess = Session() prepped = preq.prepare() # for item in req['request']['exclude_headers']: # if item in prepped.headers.keys(): # del prepped.headers[item] response = rsess.send(prepped, verify=cert_bundle, proxies=proxies) return response