Exemple #1
0
    def do_GET(self):
        m = re.fullmatch(r"/chinook/customer", self.path)
        if m:
            data = customer_list()
        else:
            m = re.fullmatch(r'/chinook/invoice/customer/(\d+)', self.path)
            if m:
                cust_id = int(m.group(1))
                data = invoices_for_customer(cust_id)
            else:
                m = re.fullmatch(r"/chinook/invoice-item/invoice/(\d+)", self.path)
                if m:
                    inv_id = int(m.group(1))
                    data = items_for_invoice(inv_id)
                else:
                    m = re.fullmatch("/chinook/invoice-item/invoice/customer", self.path)
                    if m:
                        data = items_invoices_customers()
                    else:
                        data = None

        if data:
            self.send_response(200)
            self.send_header("content-type", "text/html")
            self.end_headers()
            edat = dumps(data)
            logging.debug(edat)
            self.wfile.write(edat.encode("utf-8"))
        else:
            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"))
Exemple #2
0
    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"))
Exemple #3
0
def invoices(customer_id):
    data = invoices_for_customer(customer_id)
    response.content_type = "application/json"
    return dumps(data)
def invoices(customer_id):
    request.content_type = "application/json"
    data = invoices_for_customer(customer_id)
    return dumps(data)
Exemple #5
0
def invoices(customer_id):
    data = invoices_for_customer(customer_id)
    return dumps(data)
from services import chinook_services

customers = chinook_services.customer_list()

for customer_row in customers:
    cust_id = customer_row[0]
    invoices = chinook_services.invoices_for_customer(cust_id)

    total_invoice = 0
    num_tracks = 0
    for invoice_row in invoices:
        total_invoice += invoice_row[8]

        invoice_items = chinook_services.items_for_invoice(invoice_row[0])
        for invoiceItem_row in invoice_items:
            num_tracks += 1
    template = "{:3}  {:15}  {:15}  {:3}  {:8.2f}"
    line = template.format(cust_id, customer_row[2], customer_row[1],
                           num_tracks, total_invoice)
    print(line)