Created
March 27, 2023 15:38
-
-
Save dice89/cf92ae26b95dc2a2f45f75d1ffbbf51a to your computer and use it in GitHub Desktop.
Master Data Example Workist
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
articles = [ | |
{ | |
"article_id1": "123", | |
"description": "Some description", | |
"order_units": ["PCS"] | |
}, | |
{ | |
"article_id1": "456", | |
"description": "Language does not matter", | |
"order_units": ["KG"] | |
}, | |
{ | |
"article_id1": "789", | |
"description": "More Description in meters", | |
"order_units": ["M"] | |
} | |
] | |
clients = [ | |
{"client_id1": "C-001", | |
"company_name": "Test SE", | |
"address1": "Test street 1", | |
"address2": "Tor 2", | |
"zip_code": "1000", | |
"city": "Testhausen", | |
"country": "DE" | |
}, | |
{"client_id1": "C-002", | |
"company_name": "Muster GmbH&Co. KG", | |
"address1": "Muster street 2", | |
"address2":"empty", # because all lines need to have the same fields # TODO allow empty fields | |
"zip_code": "2000", | |
"city": "Musterstadt", | |
"country": "DE" | |
} | |
] | |
delivery_addresses = [ | |
{ | |
"partition_id": "C-001", | |
"address_id1": "L-001", | |
"name": "Tester 2 GmbH", | |
"address1": "Test street 2", | |
"zip_code": "1000", | |
"city": "Testhausen", | |
"country": "DE" | |
} | |
] | |
client_articles = [ | |
{ | |
"article_id1": "ABCD", | |
"partition_id": "C-002", | |
"description": "Client_specific", | |
"order_units": ["M"] | |
} | |
] | |
import requests | |
import json | |
base_url = "https://api.workist.com/api/v1/master-data" | |
token = "<please fill me :-)>" | |
lookup_definitions = { | |
"clients": "please fill me", | |
"articles": "please fill me", | |
"client_articles": "please fill me", | |
"delivery_addresses": "please fill me", | |
"invoice_address": None, # Not part of this example | |
"contacts": None, # Not part of this example | |
"conversion_factors": None, # Not part of this example | |
"framework-contracts": None # Not part of this example | |
} | |
# Helper function to showcase mass data imports | |
def chunk(it, size): | |
from itertools import islice | |
it = iter(it) | |
return iter(lambda: tuple(islice(it, size)), ()) | |
# Import Clients | |
url = f"{base_url}/clients/imports?replace=True" | |
payload = {"lookup_definition_id": lookup_definitions["clients"] , | |
"data": json.dumps(clients)} | |
headers = { | |
"Authorization": f"Bearer {token}" | |
} | |
response = requests.request("POST", url, headers=headers, data=payload) | |
print(json.dumps(clients)) | |
print("client import result ...") | |
print(response.text) | |
# Import Delivery Addresses | |
url = f"{base_url}/addresses/imports?replace=True" | |
payload = {"lookup_definition_id": lookup_definitions["clients"] , | |
"data": json.dumps(delivery_addresses)} | |
headers = { | |
"Authorization": f"Bearer {token}" | |
} | |
response = requests.request("POST", url, headers=headers, data=payload) | |
print(json.dumps(delivery_addresses)) | |
print("delivery address import result ...") | |
print(response.text) | |
# Import Articles (Mass data demo) | |
url = f"{base_url}/articles/imports" | |
articles_chunked = list(chunk(articles, 1)) | |
for i, articles in enumerate(articles_chunked): | |
if i == 0: | |
send_url = url + "?replace=True" | |
else: | |
send_url = url | |
payload = {"lookup_definition_id": lookup_definitions["articles"] , | |
"data": json.dumps(articles)} | |
headers = { | |
"Authorization": f"Bearer {token}" | |
} | |
response = requests.request("POST", send_url, headers=headers, data=payload) | |
print("article import result ...") | |
print(response.text) | |
# Import client Articles | |
url = f"{base_url}/articles/imports?replace=True" | |
payload = {"lookup_definition_id": lookup_definitions["clients"] , | |
"data": json.dumps(client_articles)} | |
headers = { | |
"Authorization": f"Bearer {token}" | |
} | |
response = requests.request("POST", url, headers=headers, data=payload) | |
print(json.dumps(client_articles)) | |
print("client article import result ...") | |
print(response.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment