Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/env python3 # system modules
# internal modules
""" Class for api requests """
""" class constructor Args: server (str): the server domain url (str): the url relative to the server payload [Optional(dict)]: the payload """
################## ### Properties ### ################## def logger(self): """ the logging.Logger used for logging. Defaults to logging.getLogger(__name__). """
def logger(self, logger): # pragma: no cover assert isinstance(logger, logging.Logger), \ "logger property has to be a logging.Logger" self._logger = logger
def api_connection(self): """ The http.client.HTTPSConnection used for server communication. You may set this, otherwise a new instance is created automatically on first use. """ # set it to new instance NETATMO_API_SERVER) # return internal attribute
def api_connection(self, newconn): # pragma: no cover if not isinstance(newconn, http.client.HTTPSConnection): self.logger.debug( "authentication property needs to be of " "class HTTPSConnection. Using empty instance instead " "of {}.".format(newauth)) self._api_connection = http.client.HTTPSConnection( NETATMO_API_SERVER) else: self._api_connection = newconn
def server(self): """ the api server domain """ except AttributeError: return "" # pragma: no cover
def server(self, newserver):
def url(self): """ the url relative to the server for this api request """ except AttributeError: return "" # pragma: no cover
def url(self, newurl):
def payload(self): """ the payload to send to the api """ except AttributeError: return {} # pragma: no cover
def payload(self, newpayload): except (TypeError, AssertionError): # pragma: no cover raise InvalidPayloadError("payload has to be a simple dict")
def payload_urlencoded(self): """ [read only] return the urlencoded payload (UTF-8) """
def response(self): """ the api response to this request. Instance of ApiResponse or derivates. Issues the request if necessary. You should not set this property. """
def response(self, newresponse): except (AssertionError, AttributeError): # pragma: no cover raise TypeError("response property has to be instance of " "ApiResponse or derivates.")
######################## ### context managers ### ######################## def connection_to_api(self): finally:
############### ### methods ### ############### """ Connect to the api server """
""" Disconnect from the api server """
""" Issue a POST request to the api server on the given url with the specified payload. Returns: response (dict): JSON decoded response data """ "payload {p}".format(s=self.server, u=self.url, p=self.payload_urlencoded)) # POST request to oauth/token method="POST", # a POST request url=self.url, # to this relative url body=self.payload_urlencoded, # with this payload headers=PLAIN_URLENCODED_HEADERS # and this header )
# evaluate output
def issue(self): # pragma: no cover """ Issue a POST request to the api server on the given url with the specified payload and set the 'response' property to an ApiResponse object. Subclasses may override this class to use specific derivates of ApiResponse. """ # post the request res_data = self.post_request() # pack the response into an ApiResponse object response = responsetypes.ApiResponse( response=res_data, request=self) # set the response property self.response = response
def __repr__(self): # pragma: no cover """ python representation of this object """ # self.logger.debug("__repr__ called") reprstring = ( "{classname}(\n" "server = {server},\n" "url = {url},\n" "payload = {payload},\n" ")").format( classname="{module}.{name}".format( name=self.__class__.__name__, module=self.__class__.__module__), server=self.server.__repr__(), url=self.url.__repr__(), payload=self.payload.__repr__(), ) return reprstring
""" base class for token requests """
""" class constructor Args: payload [Optional(dict)]: the payload """ server=NETATMO_API_SERVER, url=NETATMO_API_TOKEN_URL, payload=payload )
""" Issue a POST request to the api server on the Oauth2 endpoint with the specified payload and set the 'response' property to an TokenResponse object. """ # post the request # pack the response into an ApiResponse object request=self) # set the response property
""" class for Getpublicdata requests """
""" class constructor Args: payload [Optional(dict)]: the payload """ server=NETATMO_API_SERVER, url=NETATMO_API_GETPUBLICDATA_URL, payload=payload )
""" Issue a POST request to the api server on the Getpublicdata endpoint with the specified payload and set the 'response' property to an GetpublicdataResponse object. """ # post the request # pack the response into an ApiResponse object request=self) # set the response property
""" class for Getmeasure requests """
""" class constructor Args: payload [Optional(dict)]: the payload """ server=NETATMO_API_SERVER, url=NETATMO_API_GETMEASURE_URL, payload=payload )
""" Issue a POST request to the api server on the Getmeasure endpoint with the specified payload and set the 'response' property to an GetmeasureResponse object. """ # post the request # pack the response into an ApiResponse object request=self) # set the response property
""" class for Getstationsdata requests """
""" class constructor Args: payload [Optional(dict)]: the payload """ server=NETATMO_API_SERVER, url=NETATMO_API_GETSTATIONSDATA_URL, payload=payload )
""" Issue a POST request to the api server on the Getstationsdata endpoint with the specified payload and set the 'response' property to an GetstationsdataResponse object. """ # post the request # pack the response into an ApiResponse object request=self) # set the response property |