Skip to content

Commit

Permalink
adding http2 response class
Browse files Browse the repository at this point in the history
  • Loading branch information
c7h committed Jun 11, 2022
1 parent 2d5e443 commit 84e1199
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions restler/engine/transport_layer/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# Licensed under the MIT License.
import string
import re
from typing import Dict, List

import hyper
from restler_settings import Settings

DELIM = "\r\n\r\n"
Expand All @@ -14,6 +17,64 @@
# of a sequence because the sequence failed prior to that request being reached.
RESTLER_INVALID_CODE = '999'

class Http2Response(object):
def __init__(self, response: hyper.HTTP20Response):
""" Hyper response facade
"""
self._response = response
self._body = self._response.read(decode_content=True).decode('utf-8')

@property
def to_str(self):
#TODO: remove the need for this function.
# It is hacky.
return f"{self.headers}{DELIM}{self.body}"

@property
def status_code(self) -> str:
return str(self._response.status)

@property
def body(self) -> str:
return self._body

@property
def headers(self) -> str:
"""Raw response header section of response"""
h_generator = self._response.headers.iter_raw()
header_str = '\n\r'.join(f"{k.decode('utf-8')}: {v.decode('utf-8')}" for k,v in h_generator)
return header_str

@property
def headers_list(self) -> Dict:
h_dict = dict()
for k, v in self._response.headers.iter_raw():
h_dict[k] = v
return h_dict

def has_valid_code(self) -> bool:
sc = self._response.status
return sc in VALID_CODES

def has_bug_code(self) -> bool:
sc = self._response.status
custom_bug = sc in Settings().custom_non_bug_codes
fiveXX_code = sc >= 500
return custom_bug or fiveXX_code

@property
def json_body(self) -> str:
# TODO: actually parse json data
return self.body

@property
def status_text(self) -> str:
"""
This is not used in HTTP/2, and so is always an empty string.
"""
return ""


class HttpResponse(object):
def __init__(self, response_str: str=None):
""" Initializes an HttpResponse object
Expand Down

0 comments on commit 84e1199

Please sign in to comment.