Does anyone have an example on how to add a file resource to a fact sheet programatically (i.e., via APIs)?
Cheers,
Helder
Does anyone have an example on how to add a file resource to a fact sheet programatically (i.e., via APIs)?
Cheers,
Helder
Best answer by Carsten
Hi
I just got a bit time today during my lunch break. Here is some fast python code (it’s not niche, but it works), COMES WITHOUT ANY WARRANTY!:
import requests
import pandas as pd
api_token = 'YouApiToken from tech user' #AutoApproverOutOfLiveSeal
request_url = 'https://yourtennent.leanix.net/services/pathfinder/v1/graphql/upload'
auth_url = 'https://yourtennent.leanix.netservices/mtm/v1/oauth2/token'
def uploadpdf(filedata):
## Authentication:
response = requests.post(auth_url, auth=('apitoken', api_token),
data={'grant_type': 'client_credentials'})
response.raise_for_status()
access_token = response.json()['access_token']
auth_header = 'Bearer ' + access_token
header = {
'accept': 'application/json',
'Authorization': auth_header
}
form_data = {
'graphQLRequest': (
None,
'{"query":"mutation($factSheetId: ID!, $name: String!, $description: String, $url: String, $origin: String, $documentType: String, $metadata: String, $refId: String) {\\n result: createDocument(factSheetId: $factSheetId, name: $name, description: $description, url: $url, origin: $origin, documentType: $documentType, metadata: $metadata, refId: $refId) {\\n id name description url createdAt fileInformation { fileName size mediaType previewImage content } origin documentType metadata refId\\n }\\n }","variables":{"factSheetId":"0ec6bf5d-e97e-48ac-a1b2-6db1fa5e5a8a","documentType":"documentation","name":"test.pdf","description":null,"url":null,"origin":"LX_STORAGE_SERVICE"}}'
),
'file': ('test.pdf', filedata, 'application/pdf')
}
response = requests.post(request_url, headers=header, files=form_data)
response.raise_for_status()
print(response)
def loadpdf():
with open('C:\\temp\\test.pdf', 'rb') as f:
return f.read()
if __name__ == "__main__":
uploadpdf(loadpdf())
If I had more time I’d made it nicer, but so I left you some work to do ;-)
Depending on the filedata type you have to adjust the file content type e.g. from 'application/pdf' to 'application/png'
Anyway running the DevTools of your browser (section network) will help you for variations.
I hope this helps,
best regards,
Carsten
No account yet? Create an account
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.