host_url = "http://{}/weather/{}/forecast".format(request.host, postalCode) cliResp = requests.request( method=request.method, url=host_url, headers={key: value for (key, value) in request.headers if key != 'Host'}, data=request.body, allow_redirects=False) if cliResp.status_code != 200: return HttpResponse.errorResponse(cliResp.status_code, "Message") logging.info(cliResp.text) response = HttpResponse.okResponse() response.headers.append(("Cache-Control", "private")) response.addContentLengthHeader(len(cliResp.text)) response.addContentTypeHeader("application/xml; charset=utf-8") response.addServerHeader() response.addRequestContextHeader() response.addAccessControlHeader() response.addDateHeader() response.body = cliResp.text return response addUrl("/weather/(?P<postalCode>.+)/forecast$", urlWeather)
from datetime import datetime from httpobj import HttpRequest, HttpResponse, addUrl def urlTime(request): utc = datetime.utcnow() strDate = utc.strftime("%Y-%m-%dT%H:%M:%SZ") timeXmlStr = '<time version="1.42" xmlns:atom="http://www.w3.org/2005/Atom"><atom:link rel="self" href="http://www.api.ing.carrier.com/time/"/><utc>' timeXmlStr += strDate timeXmlStr += '</utc></time>' response = HttpResponse.okResponse() response.headers.append(("Cache-Control", "private")) response.addContentLengthHeader(len(timeXmlStr)) response.addContentTypeHeader("application/xml; charset=utf-8") response.addServerHeader() response.addRequestContextHeader() response.addAccessControlHeader() response.addDateHeader() response.body = timeXmlStr return response addUrl("/time/", urlTime)
with open("manifest.xml", 'r') as fhan: for line in fhan: responseManifest = responseManifest + line loadXMLFiles() def urlManifest(request): global responseManifest response = HttpResponse.okResponse() response.headers.append(("Cache-Control", "no-store,no-cache")) response.headers.append(("Pragma", "no-cache")) response.addContentLengthHeader(len(responseManifest)) response.addContentTypeHeader("application/xml") response.addRequestContextHeader() # Set-cookie header response.addDateHeader() response.body = responseManifest return response addUrl("/manifest", urlManifest)
# # /time URL handling # # This returns a simple plain text response and is checked by the thermostat # periodically. # from httpobj import HttpRequest, HttpResponse, addUrl def urlAlive(request): response = HttpResponse.okResponse() response.headers.append(("Cache-Control", "private, no-transform")) response.addContentLengthHeader(5) response.addContentTypeHeader("text/plain; charset=utf-8") response.addServerHeader() response.addRequestContextHeader() response.addAccessControlHeader() response.addDateHeader() response.body = "alive" return response addUrl("/Alive$", urlAlive)
if not testVal.is_integer(): logging.info("temp value must be in 0.5 increments: %s", tempValue) return makeApiResponse(400, "temp value must be 0.5 increments", None) urlsystems.pendingActionHold = True urlsystems.pendingActionActivity = activityValue urlsystems.pendingActionUntil = untilValue urlsystems.pendingActionTemp = tempValue logging.info("Set pending hold=on to {} until {} temp {}".format(urlsystems.pendingActionActivity, urlsystems.pendingActionUntil, urlsystems.pendingActionTemp)) return makeApiResponse(200, "OK", None) addUrl("/api/hold/(?P<zoneId>.+)$", urlApiZoneSetHold) def urlApiGetZoneField(request): zoneId = request.pathDict['zoneId'] fieldName = request.pathDict['fieldName'] if zoneId not in urlsystems.statusZones: return makeApiResponse(404, "No data", None) zoneObj = urlsystems.statusZones[zoneId] if fieldName not in zoneObj:
from httpobj import HttpRequest, HttpResponse, addUrl def urlRelNodes(request): hostAndPath = request.pathDict['hostAndPath'] logging.info("Fetch http://{}".format(hostAndPath)) bodyStr = "Returned from python server" response = HttpResponse.okResponse() response.headers.append(("Cache-Control", "no-store,no-cache")) response.headers.append(("Pragma", "no-cache")) response.addContentLengthHeader(len(bodyStr)) response.addContentTypeHeader("text/plain") response.addRequestContextHeader() response.headers.append(("X-Content-CRC", "1278")) response.headers.append(("X-Current-Page", "http://{}".format(hostAndPath))) # Cookie response.addDateHeader() response.body = bodyStr return response addUrl("http://(?P<hostAndPath>.+)$", urlRelNodes)
logging.debug(" SN={}".format(request.pathDict["serialNumber"])) logging.debug(" body={}".format(xmlBodyStr)) response = HttpResponse.okResponse() response.headers.append(("Cache-Control", "private")) response.addServerHeader() response.addRequestContextHeader() response.addAccessControlHeader() response.addDateHeader() response.addContentLengthHeader(0) return response addUrl("/systems/(?P<serialNumber>.+)/profile$", urlSystemsProfile) # Device tells us the dealer information that has been programmed into the # device. (We do not send back any information.) def urlSystemsDealer(request): xmlBodyStr = request.bodyDict["data"][0] logging.debug(" SN={}".format(request.pathDict["serialNumber"])) logging.debug(" body={}".format(xmlBodyStr)) response = HttpResponse.okResponse() response.headers.append(("Cache-Control", "private")) response.headers.append(("Etag", "\"00f5713108d7b88afec10590\""))