Rest API Examples using Python

TOP

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
__requires__ = "Request module (https://github.com/requests/requests)"

import requests
import json
from time import sleep

url = 'https://app.ezzydoc.com/EzzyService.svc/Rest'
api_key = {'APIKey': '#INSERT API KEY#'}
payload = {'user': '#INSERT USER NAME#', 'pwd': '#INSERT PASS WORD#', 'APIKey': '#INSERT API KEY#'}

# Log in to EzzyBills
r = requests.get(url + '/Login', params=payload)

# Upload an invoice to EzzyBills
with open('taxinvoice.pdf', 'rb') as img_file:
    img_name = 'taxinvoice.pdf'
    data = img_file.read()
    b = bytearray(data)
    li = []
    for i in b:
        li.append(i)

    raw_data = {"PictureName": "taxinvoice.pdf", "PictureStream": li}
    json_data = json.dumps(raw_data)

    r2 = requests.post(
        "https://app.ezzydoc.com/EzzyService.svc/Rest/uploadInvoiceImage",
        data=json_data, cookies=r.cookies, params=api_key, headers={'Content-Type': 'application/json'})
    invoiceID = str(r2.json().get("invoice_id"))

# Wait until invoice has been processed
while True:
    r4 = requests.get(url + '/workflowStatus?invoiceid=' + invoiceID, cookies=r.cookies, params=api_key)
    completeBool = str(r4.json().get("complete"))
    if completeBool == "True":
        break
    sleep(1)

# Get invoice total
r5 = requests.get(url + '/getInvoiceHeaderBlocks?invoiceid=' + invoiceID, cookies=r.cookies, params=api_key)
invoiceTotal = str(r5.json().get("invoiceForm").get("invoiceTotal"))
print("Invoice total is: $" + str(invoiceTotal))

Bottom