Package: kladama-api
Kladama API for PythonPython API for Kladama Services IntegrationHow to install itYou can install the API through PIPpipinstallkladama-apiHow to start using it. "List available Variables" example.First, you must to authenticate through aAPI Tokenthat must be provided to you. This authentication process returns a session object that must be used to create aContextobject.# retrieve all available variablesimportkladamaaskldenv=kld.Environments().prodapi_token='<your provided token>'session=kld.authenticate(env,api_token)query=kld.Queries().varresponse=kld.Context(session).get(query)ifisinstance(response,kld.Success):forvarinresponse.result:print(var.name,'-',var.description,'in',var.link)How to check if a GeoJson data can be used as a valid AoI in Kladamaimportkladamaaskldenv=kld.Environments().prodapi_token='<your provided token>'session=kld.authenticate(env,api_token)query=kld.Services().validate_aoi({"type":"FeatureCollection","features":[{"type":"Feature","properties":{"id":"897d8348a539983c4f435b45","name":"field-1"},"geometry":{"type":"Polygon","coordinates":[[[-50.675417,-22.000000],[-10.675394,-21.000000],[-30.669532,-21.000000],[-50.656133,-21.000000],[-60.656118,-21.000000],[-50.675417,-22.000000]]]}}]})response=kld.Context(session).get(query)ifisinstance(response,kld.Error):print(response.__str__())else:print('Valid: ',response.result['valid'])formessageinresponse.result['messages']:print(message)How to add an area of interest (AoI)importkladamaaskldenv=kld.Environments().prodapi_token='<your provided token>'session=kld.authenticate(env,api_token)transaction=kld.Transactions()\.add_aoi\.for_user('<your user>')\.with_name('<aoi name>')\.with_description("Test AOI")\.with_category("Test")\.with_features({"type":"FeatureCollection","name":"Test AoI","features":[{"type":"Feature","properties":{"id":"897d8348a539983c4f435b45","name":"field-1"},"geometry":{"type":"Polygon","coordinates":[[[-50.675417,-22.000000],[-10.675394,-21.000000],[-30.669532,-21.000000],[-50.656133,-21.000000],[-60.656118,-21.000000],[-50.675417,-22.000000]]]}}]})response=kld.Context(session).execute(transaction)ifnotisinstance(response,kld.Success):print(response.__str__())# Also, you can add an AoI from a JSON filetransaction=kld.Transactions()\.add_aoi\.for_user('<your user>')\.with_name('<aoi name>')\.with_description("Test AOI")\.with_category("Test")\.from_file('<JSON filename>')How to create a Periodic Subscription to a variable# create periodic subscriptionimportkladamaaskldenv=kld.Environments().prodapi_token='<your provided token>'session=kld.authenticate(env,api_token)# could be any other Spatial Operation (MIN, MAX, STD, etc)transaction=kld.Transactions()\.periodic_subsc\.for_user('<your user>')\.with_variable('<var name>')\.with_operation('MEAN')\.with_aoi('<aoi name>')response=kld.Context(session).execute(transaction)ifisinstance(response,kld.Success):ifresponse.type==kld.ResultType.OK:code=response.result['code']# property 'code' contains ID of new created subscriptionprint("New subscription code: ",code)else:print("Redirect to: ",response.result['href'])# property 'href' contains URI of identical subscription resourceelse:print('Error code: ',response.code,'Message: ',response.message)How to get last data from a periodic subscriptionimportkladamaaskldenv=kld.Environments().prodapi_token='<your provided token>'session=kld.authenticate(env,api_token)query=kld.Queries()\.subsc\.by_user('<your user>')\.by_key('<subscription code>')\.results\.lastresponse=kld.Context(session).get(query)ifisinstance(response,kld.Error):print(response.__str__())elifresponse.resultisNone:print('response is successful but empty')else:assertisinstance(response.result,kld.BinaryResult)print('Saving to file: ',response.result.name)withopen(response.result.name,mode='wb')asfh:fh.write(response.result.content)