Skip to content

Instantly share code, notes, and snippets.

@Day0Dreamer
Created November 23, 2018 23:14
Show Gist options
  • Save Day0Dreamer/a5949fc7ce2111a235875cc939fab10c to your computer and use it in GitHub Desktop.
Save Day0Dreamer/a5949fc7ce2111a235875cc939fab10c to your computer and use it in GitHub Desktop.
import requests
import json
import urllib.parse
URL = "http://10.10.10.105/link/"
PROXY = "http://10.10.10.105/link/proxy/"
class Request(object):
def __init__(self, class_url, request_type, request_class, response_class, headers, data, url=URL, proxy=PROXY):
"""
:type class_url: string
:param class_url: for example "chats/system"
:type request_type: string
:param request_type: for example "GET", "POST", "SET"
:type request_class: string
:param request_class: for example "Chats.ChatInfos"
:type response_class: string
:param response_class: for example "Chats.GetMesagesResponse"
:return: Response object
:rtype: requests.Response
"""
self.class_url = class_url
self.request_type = request_type
self.request_class = request_class
self.response_class = response_class
# Making url like https://kurshigames.ru/link/api/attributes
self.params = {
"url": "{URL}{API}{CLASS_URL}".format(**{'URL': URL.rstrip('/')+'/',
'API': 'api'.rstrip('/')+'/',
'CLASS_URL': class_url.rstrip('/')+'/'}),
"requestClass": "Twocookies.API.{}".format(request_class),
"responseClass": "Twocookies.API.{}".format(response_class)
}
# Making a header
self.headers = {**{
'Content-Type': "application/proto-binary",
'Accept': "application/proto-binary",
}, **headers}
self.data = data
# Convert dictionary to json-sting unless it's a string already
if isinstance(self.data, dict):
data = json.dumps(self.data)
elif isinstance(self.data, str):
try:
# Check if incoming JSON string is decodable
json.loads(self.data)
except json.JSONDecodeError as e:
print('JSON string used is not a valid json format')
raise e
else:
raise ValueError('Only JSON-strings and dicts are accepted')
def run(self):
return requests.post(
PROXY,
data=self.data,
headers=self.headers,
params=self.params,
hooks={'response': self.print_url})
#
# return requests.request(self.request_type,
# PROXY,
# data=self.data,
# headers=self.headers,
# params=self.params,
# hooks={'response': self.print_url})
def print_url(self, r, *args, **kwargs):
print(urllib.parse.unquote(r.url))
response = Request(class_url='chats/system',
request_type='POST',
request_class='Chats.ChatInfos',
response_class='Chats.GetMesagesResponse',
headers={'token': "5bf7e7db0a92ce8278ea68ad"},
data={"Name": "system",
"LastMessage": 0}
)
# print(response.json())
# print(response.raise_for_status())
print(response.run().status_code)
print(response.run().text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment