Skip to main content

Does anyone have an example on how to add a file resource to a fact sheet programatically (i.e., via APIs)?

 

Cheers,

 

Helder

 

Ideally with Python library.

 


Hello @Helder.Luz ,
i did not try it out in python (just swagger), but the correct API is in the pathfinder service, I used it to upload a png image:

It is a bit tricky.

The graphql is a mutation like:
    {"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":"image","name":"Screenshot 2024-06-19 145337.png","description":null,"url":null,"origin":"LX_STORAGE_SERVICE"}}

When you use python, I guess you will put the binary data into your json:

form_data = {

     ‘file’:(’Screenshot 2024-06-19 145337.png’,BinaryData,’image/png’),

     ‘graphQLRequest’:(….


I hope that helps,

best regards,

Carsten


Hi @Helder.Luz ,
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()c'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


Thanks Carsten

Appreciate that. I think we identified the issue is the way that the gql is handling multipart-file header that causes an issue when used against the LeanIX API. So just re-trying that tweak now.


Many thanks @Carsten! Great help.


Hello community!
Since we see this question asked more often, we will update our documentation 😎 providing further details on how to perform this type of operations programmatically.


Reply