Skip to content

Commit

Permalink
added metaclass interface for response
Browse files Browse the repository at this point in the history
  • Loading branch information
c7h committed Jun 11, 2022
1 parent 3f3b2f9 commit b19c336
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions restler/engine/transport_layer/response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
from abc import ABCMeta, abstractmethod, abstractproperty
import string
import re
from typing import Dict, List
Expand All @@ -17,15 +18,62 @@
# of a sequence because the sequence failed prior to that request being reached.
RESTLER_INVALID_CODE = '999'

class Http2Response(object):
class AbstractHttpResponse(object, metaclass=ABCMeta):
@abstractmethod
def __init__(self, response):
pass

@abstractproperty
def to_str(self):
pass

@abstractproperty
def to_str(self) -> str:
pass

@abstractproperty
def status_code(self) -> str:
pass

@abstractproperty
def body(self) -> str:
pass

@abstractproperty
def headers(self) -> str:
"""Raw response header section of response"""
pass

@abstractproperty
def headers_list(self) -> Dict:
pass

@abstractmethod
def has_valid_code(self) -> bool:
pass

@abstractmethod
def has_bug_code(self) -> bool:
pass

@abstractproperty
def json_body(self) -> str:
pass

@abstractproperty
def status_text(self) -> str:
pass


class Http2Response(AbstractHttpResponse):
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):
def to_str(self) -> str:
#TODO: remove the need for this function.
# It is hacky.
return f"{self.headers}{DELIM}{self.body}"
Expand Down Expand Up @@ -75,7 +123,7 @@ def status_text(self) -> str:
return ""


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

0 comments on commit b19c336

Please sign in to comment.