def request_to_sakila(path="", body=None, method='GET', headers=None): from http.client import HTTPConnection, HTTPResponse import json_with_dates #print('path', path) if body: jbody = json_with_dates.dumps(body) enjbody = jbody.encode() else: enjbody = None hconn = HTTPConnection('localhost', 80) if headers: hconn.request(method, '/cgi-bin/sakila_rest/sakila.py' + path, body=enjbody, headers=headers) else: hconn.request(method, '/cgi-bin/sakila_rest/sakila.py' + path, body=enjbody) resp = hconn.getresponse() #print('status', resp.status) bodyJ = resp.read() #print('body',body) bodystr = bodyJ.decode() if resp.status == 200: #data = json_with_dates.loads(bodystr) #return data #json_with_dates.loads(hconn.getresponse().read().decode()) body = json_with_dates.loads(bodystr) return ('K',body) else: return ('E', resp.status, bodystr) #print("response status", resp.status) #print('bodystr', bodystr)
def send_data(data): data_j = dumps(data) logging.debug("custJ " + str(data_j)) lng = len(data_j) logging.debug("len " + str(lng)) print("Content-Type: application/json; charset=UTF-8") print("Content-Length: " + str(lng)) print() print(data_j)
def do_GET(self): pass # decide which path is being used # self.path is the path from the request m = re.fullmatch("/chinook/customer", self.path) if m: # matches the first regex data = chinook_services.customer_list() else: # did not match m = re.fullmatch(r"/chinook/invoice/customer/(\d+)", self.path) if m: cust_id = int(m.group(1)) data = chinook_services.invoices_for_customer(cust_id) else: m = re.fullmatch(r"/chinook/invoice-item/invoice/(\d+)", self.path) if m: invoice_id = int(m.group(1)) data = chinook_services.items_for_invoice(invoice_id) else: m = re.fullmatch(r"/chinook/items/invoices/customers", self.path) data = chinook_services.items_invoices_customers() if m: pass else: data = None # send appropriate response back if data: # path matched, good response self.send_response(200) self.send_header("content-type", "application/json") self.end_headers() jdata = json_with_dates.dumps(data) self.wfile.write(jdata.encode("UTF-8")) else: # path did not match self.send_response(404) self.send_header("content-type", "text/html") self.end_headers() self.wfile.write("invalid path: {}".format( self.path).encode("utf-8"))
def customers(): data = customer_list() response.content_type = 'application/json' return dumps(data)
def items(customer_id): response.content_type = "application/json" data = invoice_items_for_customer(customer_id) return dumps(data)
def items_invoices_customers_all(): response.content_type = "application/json" data = items_invoices_customers() return dumps(data)
def area(): area = get_all_areas() response.content_type = "application/json" return dumps(area)
def locations_by_area(area_id): response.content_type = "application/json" data = number_of_locations_by_area(area_id) return dumps(data)
def measurements_in_location(location_id): response.content_type = "application/json" data = get_measurements_for_location(location_id) return dumps(data)
def location(loc_id): response.content_type = "application/json" data = get_location(loc_id) return dumps(data)
def invoices(customer_id): request.content_type = "application/json" data = invoices_for_customer(customer_id) return dumps(data)
def invoices(customer_id): data = invoices_for_customer(customer_id) return dumps(data)
def numb_of_locations_by_area(area_id): num_location = number_of_locations_by_area(area_id) response.content_type = "application/json" return dumps(num_location)
def categories_for_area(area_id): categories_area = get_categories_for_area(area_id) response.content_type = "application/json" return dumps(categories_area)
def area(): response.content_type = "application/json" data = get_all_areas() return dumps(data)
def items(invoice_id): data = items_for_invoice(invoice_id) return dumps(data)
def area(area_id): response.content_type = "application/json" data = get_area(area_id) return dumps(data)
def items_invoices_customers_all(): data = items_invoices_customers() return dumps(data)
def location_area(area_id): data = get_locations_for_area(area_id) response.content_type = "application/json" return dumps(data)
def items(customer_id): data = invoice_items_for_customer(customer_id) return dumps(data)
def categories_in_area(area_id): response.content_type = "application/json" data = get_categories_for_area(area_id) return dumps(data)
def customers(): data = customer_list() return dumps(data)
def average_measurements(area_id): response.content_type = "application/json" data = get_average_measurements_for_area(area_id) return dumps(data)
from json_with_dates import loads, dumps def request_to_sakila(path="", body=None, method='GET'): from http.client import HTTPConnection, HTTPResponse import json_with_dates #print('path', path) hconn = HTTPConnection('localhost', 82) hconn.request(method, 'http://localhost/cgi-bin/sakila_rest/sakila.py' + path, body=body, headers={'content-type': 'applicaton/json'}) resp = hconn.getresponse() #print('status', resp.status) bodyJ = resp.read() #print('body',body) bodystr = bodyJ.decode() if resp.status == 200: #data = json_with_dates.loads(bodystr) #return data #json_with_dates.loads(hconn.getresponse().read().decode()) body = loads(bodystr) return body else: print("response status", resp.status) print('bodystr', bodystr) values = { 'first_name': 'John', 'last_name': 'Doe', 'email': '[email protected]', } jval = dumps(values) request_to_sakila(path="/customer", body=jval, method='POST')