From 6e7ed8333fec95e3f340bf1fdbb2856ba6e0146a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?poljar=20=28Damir=20Jeli=C4=87=29?= Date: Fri, 26 Jan 2018 13:50:26 +0100 Subject: [PATCH] Split up http handling into a separate file. --- matrix/http.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++ weechat-matrix.py | 72 +------------------------------------------- 2 files changed, 78 insertions(+), 71 deletions(-) create mode 100644 matrix/http.py diff --git a/matrix/http.py b/matrix/http.py new file mode 100644 index 0000000..d48e1da --- /dev/null +++ b/matrix/http.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- + +from __future__ import unicode_literals + +import json +from enum import Enum, unique + + +@unique +class RequestType(Enum): + GET = 0 + POST = 1 + PUT = 2 + + +class HttpResponse: + def __init__(self, status, headers, body): + self.status = status # type: int + self.headers = headers # type: Dict[str, str] + self.body = body # type: bytes + + +class HttpRequest: + def __init__( + self, + request_type, # type: RequestType + host, # type: str + port, # type: int + location, # type: str + data=None, # type: Dict[str, Any] + user_agent='weechat-matrix/{version}'.format( + version="0.1") # type: str + ): + # type: (...) -> None + host_string = ':'.join([host, str(port)]) + + user_agent = 'User-Agent: {agent}'.format(agent=user_agent) + host_header = 'Host: {host}'.format(host=host_string) + request_list = [] # type: List[str] + accept_header = 'Accept: */*' # type: str + end_separator = '\r\n' # type: str + payload = None # type: str + + if request_type == RequestType.GET: + get = 'GET {location} HTTP/1.1'.format(location=location) + request_list = [get, host_header, + user_agent, accept_header, end_separator] + + elif (request_type == RequestType.POST or + request_type == RequestType.PUT): + + json_data = json.dumps(data, separators=(',', ':')) + + if request_type == RequestType.POST: + method = "POST" + else: + method = "PUT" + + request_line = '{method} {location} HTTP/1.1'.format( + method=method, + location=location + ) + + type_header = 'Content-Type: application/x-www-form-urlencoded' + length_header = 'Content-Length: {length}'.format( + length=len(json_data) + ) + + request_list = [request_line, host_header, + user_agent, accept_header, + length_header, type_header, end_separator] + payload = json_data + + request = '\r\n'.join(request_list) + + self.request = request + self.payload = payload diff --git a/weechat-matrix.py b/weechat-matrix.py index 730546a..64b0712 100644 --- a/weechat-matrix.py +++ b/weechat-matrix.py @@ -25,6 +25,7 @@ from http_parser.pyparser import HttpParser from matrix import colors from matrix.utf import WeechatWrapper, utf8_decode +from matrix.http import HttpRequest, HttpResponse, RequestType # pylint: disable=import-error import weechat @@ -55,13 +56,6 @@ class MessageType(Enum): INVITE = 8 -@unique -class RequestType(Enum): - GET = 0 - POST = 1 - PUT = 2 - - @unique class RedactType(Enum): STRIKETHROUGH = 0 @@ -116,70 +110,6 @@ class PluginOptions: self.debug = [] # type: DebugType -class HttpResponse: - def __init__(self, status, headers, body): - self.status = status # type: int - self.headers = headers # type: Dict[str, str] - self.body = body # type: bytes - - -class HttpRequest: - def __init__( - self, - request_type, # type: RequestType - host, # type: str - port, # type: int - location, # type: str - data=None, # type: Dict[str, Any] - user_agent='weechat-matrix/{version}'.format( - version=WEECHAT_SCRIPT_VERSION) # type: str - ): - # type: (...) -> None - host_string = ':'.join([host, str(port)]) - - user_agent = 'User-Agent: {agent}'.format(agent=user_agent) - host_header = 'Host: {host}'.format(host=host_string) - request_list = [] # type: List[str] - accept_header = 'Accept: */*' # type: str - end_separator = '\r\n' # type: str - payload = None # type: str - - if request_type == RequestType.GET: - get = 'GET {location} HTTP/1.1'.format(location=location) - request_list = [get, host_header, - user_agent, accept_header, end_separator] - - elif (request_type == RequestType.POST or - request_type == RequestType.PUT): - - json_data = json.dumps(data, separators=(',', ':')) - - if request_type == RequestType.POST: - method = "POST" - else: - method = "PUT" - - request_line = '{method} {location} HTTP/1.1'.format( - method=method, - location=location - ) - - type_header = 'Content-Type: application/x-www-form-urlencoded' - length_header = 'Content-Length: {length}'.format( - length=len(json_data) - ) - - request_list = [request_line, host_header, - user_agent, accept_header, - length_header, type_header, end_separator] - payload = json_data - - request = '\r\n'.join(request_list) - - self.request = request - self.payload = payload - - def get_transaction_id(server): # type: (MatrixServer) -> int transaction_id = server.transaction_id