Created
May 6, 2021 14:48
-
-
Save clemsos/4398cee924d3056d1f5838f95bf0cb4a to your computer and use it in GitHub Desktop.
Import data into Crater craterapp.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
import json | |
# set headers for lavarel | |
headers = { "Accept": "application/json" } | |
# session to store cookies | |
sess = requests.Session() | |
def createUser(): | |
user = { | |
"name": "The Importer", | |
"email" : "[email protected]", | |
"password" : "so-secure-crazy!" | |
} | |
r = sess.post("http://localhost/api/v1/users", json=user, headers=headers) | |
def getToken(): | |
# get sanctum crsf token | |
r = sess.get("http://localhost/sanctum/csrf-cookie") | |
plain_cookie = r.headers['Set-Cookie'] | |
dict_cookie = [] | |
for p in plain_cookie.split('; ') : | |
match = re.match(r'^([^=]+)=(.+)$', p.strip()) | |
if match: | |
dict_cookie.append(match.groups()) | |
cj = requests.utils.cookiejar_from_dict( dict(dict_cookie) ) | |
# add crsf to session cookies | |
sess.cookies = cj | |
# login | |
url_login = "http://localhost/api/v1/auth/login" | |
data = { | |
"username": "[email protected]", | |
"password": "cratercrater", | |
"device_name" : "python" | |
} | |
login_request = sess.post(url_login, json=data, headers=headers) | |
# get auth token | |
data = login_request.json() | |
print(data) | |
return data["token"] | |
def getUser(): | |
r = sess.get("http://localhost/api/v1/me", headers=headers) | |
user = r.json() | |
return user | |
# token = getToken() | |
token = "11|jnsX4sBaCujaS946vBuIjihquMta8UwKNEONX5ed" | |
print(token) | |
# add Bearer auth | |
headers["Authorization"] = "Bearer %s" % (token) | |
# get user | |
# user = getUser() | |
# get inmvoice number length from company settings (-- not working) | |
settings = 'invoice_prefix,invoice_number_length' | |
r = sess.get("http://localhost/api/v1/company/settings", params={ "settings[]": settings }, headers=headers) | |
print(r.json()) | |
invoice_number_length = 6 | |
# write an invoice record | |
url_invoices = "http://localhost/api/v1/invoices" | |
# parse invoice number | |
invoice_number = "0"*(invoice_number_length-1)+"2" | |
print(invoice_number) | |
# item for the invoice | |
item = { | |
"invoice_id": None, | |
"item_id": None, | |
"name": 'lol', | |
"title": '', | |
"description": None, | |
"quantity": 1, | |
"price": 0, | |
"discount_type": 'fixed', | |
"discount_val": 0, | |
"discount": 0, | |
"total": 0, | |
"totalTax": 0, | |
"totalSimpleTax": 0, | |
"totalCompoundTax": 0, | |
"tax": 0, | |
"taxes": [], | |
} | |
invoice = { | |
"invoice_date": "2020-12-03", | |
"due_date": "2020-12-04", | |
"user_id": 1, | |
"invoice_number": invoice_number, | |
"discount": 0, | |
"discount_val": 0, | |
"sub_total": 1200, | |
"total": 1200, | |
"tax": [], | |
"invoice_template_id": 1, | |
"items": [ item ] | |
} | |
r = sess.post(url_invoices, json=invoice, headers=headers) | |
print(r.text) | |
# get invoices | |
r = sess.get(url_invoices, headers=headers) | |
print(r.json()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment