Split up http handling into a separate file.
This commit is contained in:
parent
85f3a9eef0
commit
6e7ed8333f
2 changed files with 78 additions and 71 deletions
77
matrix/http.py
Normal file
77
matrix/http.py
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue