* switching to building options in query string
* payloads for things like /message/send are now send through the request body * no longer sending body data in gets This whole patch is basically so we can allow js clients to exist btw -_-
This commit is contained in:
		
							parent
							
								
									21b184b324
								
							
						
					
					
						commit
						f585cf122c
					
				@ -2,6 +2,8 @@ import sys
 | 
			
		||||
import requests
 | 
			
		||||
import time
 | 
			
		||||
import json
 | 
			
		||||
from urllib.parse import quote
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class RequestError(Exception):
 | 
			
		||||
    pass
 | 
			
		||||
@ -12,7 +14,7 @@ class Response:
 | 
			
		||||
    Primarily created by Requests that get `make`'d.
 | 
			
		||||
    '''
 | 
			
		||||
    def __init__(self, method: str, url: str, body: str, code: int, expected: int, out=sys.stdout, color=True,
 | 
			
		||||
                 truncate_long_body=True):
 | 
			
		||||
                 truncate_long_body=True, p=None):
 | 
			
		||||
 | 
			
		||||
        self.method = method
 | 
			
		||||
        self.url = url
 | 
			
		||||
@ -23,6 +25,7 @@ class Response:
 | 
			
		||||
        self.out = out # file handle to write to normally sys.stdout
 | 
			
		||||
        self.color = color # bool telling if log should color anything (on by default)
 | 
			
		||||
        self.truncate_long_body = truncate_long_body
 | 
			
		||||
        self.raw_params = p
 | 
			
		||||
 | 
			
		||||
    def _color(self, cc, string):
 | 
			
		||||
        nc = '\033[0m'
 | 
			
		||||
@ -57,11 +60,14 @@ class Response:
 | 
			
		||||
 | 
			
		||||
    def log(self):
 | 
			
		||||
        if self.code != self.expected:
 | 
			
		||||
            msg = f'Failed {self.method.upper()} {self.url}\t{self.code} expected {self.expected}'
 | 
			
		||||
            base_url = self.url[:self.url.find('?')]
 | 
			
		||||
            fail = f'Failed {self.method.upper()} {base_url}\t{self.code} expected {self.expected}\n'
 | 
			
		||||
            content = f'\tRaw params: {self.raw_params[0]}\n\tParsed Params: {self.raw_params[1]}'
 | 
			
		||||
            if self.color:
 | 
			
		||||
                msg = self._color_failing(msg)
 | 
			
		||||
                fail = self._color_failing(fail)
 | 
			
		||||
 | 
			
		||||
            self.__write_msg(msg)
 | 
			
		||||
            self.__write_msg(fail)
 | 
			
		||||
            self.__write_msg(content)
 | 
			
		||||
            self._log_body()
 | 
			
		||||
        else:
 | 
			
		||||
            msg = f'Passing: {self.method} {self.url}'
 | 
			
		||||
@ -93,22 +99,43 @@ class Request:
 | 
			
		||||
 | 
			
		||||
        self.method = method
 | 
			
		||||
        self.url    = url
 | 
			
		||||
        self.params = params
 | 
			
		||||
        if 'content' in params:
 | 
			
		||||
            self.body = params['content']
 | 
			
		||||
        else:
 | 
			
		||||
            self.body = None
 | 
			
		||||
 | 
			
		||||
        self.query_string = ''
 | 
			
		||||
        self.qs_dict = params
 | 
			
		||||
        for key in params:
 | 
			
		||||
            # percent encode all of our values before we construct the query string
 | 
			
		||||
            key = quote(key)
 | 
			
		||||
            value = quote(str(params[key]))
 | 
			
		||||
            if key == 'content':
 | 
			
		||||
                self.body = params['content']
 | 
			
		||||
            else:
 | 
			
		||||
                value = params[key]
 | 
			
		||||
                if len(self.query_string) == 0:
 | 
			
		||||
                    self.query_string += f'?{key}={value}'
 | 
			
		||||
                else:
 | 
			
		||||
                    self.query_string += f'&{key}={value}'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def _make_request(self, method: str, hope: int):
 | 
			
		||||
        # Lower driver for actuall making the request we are looking for
 | 
			
		||||
        method = method.lower()
 | 
			
		||||
 | 
			
		||||
        params = json.dumps(self.params)
 | 
			
		||||
        url = self.url + self.query_string
 | 
			
		||||
        raw_params = (self.query_string, self.qs_dict)
 | 
			
		||||
        if method == 'get':
 | 
			
		||||
            resp = requests.get(self.url, data=params)
 | 
			
		||||
            return Response('get', self.url, resp.text, resp.status_code, hope)
 | 
			
		||||
            resp = requests.get(url)
 | 
			
		||||
            return Response('get', url, resp.text, resp.status_code, hope, p=raw_params)
 | 
			
		||||
        elif method == 'post':
 | 
			
		||||
            resp = requests.post(self.url, data=params)
 | 
			
		||||
            return Response('post', self.url, resp.text, resp.status_code, hope)
 | 
			
		||||
            resp = requests.post(url, data=self.body)
 | 
			
		||||
            return Response('post', url, resp.text, resp.status_code, hope, p=raw_params)
 | 
			
		||||
        elif method == 'delete':
 | 
			
		||||
            resp = requests.delete(self.url, data=params)
 | 
			
		||||
            return Response('delete', self.url, resp.text, resp.status_code, hope)
 | 
			
		||||
            resp = requests.delete(url)
 | 
			
		||||
            return Response('delete', url, resp.text, resp.status_code, hope, p=raw_params)
 | 
			
		||||
 | 
			
		||||
        else:
 | 
			
		||||
            raise RequestError('Invalid method passed')
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user