Package: canopy
InstallationVersioningThis library usesSimVerversioning, where a change in the major version number indicates a
breaking change and a change in the minor version number indicates a non-breaking change (such as an additional
feature or bug fix).ChangelogThe changelog is availablehere.Requirements.This library has been tested on Python 3.6 and higher.pip installpipinstallcanopyYou may need to runpipwith root permission:sudo pip install canopy.From a Jupyter Notebook you can run!pip install canopy.SetuptoolsInstall viaSetuptools.pythonsetup.pyinstall--user(orsudo python setup.py installto install the package for all users)Running TestsUnit tests can be run with:pytest canopyIntegration tests can be run with:pytest integration_testsTo run the integration tests you'll need to ensure you have an environment variable calledCANOPY_PYTHON_INTEGRATION_TEST_CREDENTIALScontaining the string<client_id>|<client_secret>|<username>|<tenant_name>|<password>.Getting StartedExample UsageSee theCanopy Python Examplesrepository for example usage.IntroductionThis package is designed for customers ofCanopy Simulationswho would like
to access the Canopy API from Python, for example using Jupyter Notebooks.Currently the library is split into two parts:The client generated using the OpenAPI toolset is located in the "canopy/openapi" folder.
We don't have a great deal of control over how this code looks, but it should give a fairly complete interface to the main API.One folder up from that in the "canopy" folder we are adding helper functions which wrap common use cases in simple functions.
You can also use these functions as a reference to using the OpenAPI generated code.When using the library you generally start by creating acanopy.Sessionobject.
The session object manages authentication, and the caching of user settings.
Callingsession.authentication.authenticate()before calling OpenAPI generated client functions ensures that you are
authenticated and that any expired access tokens are refreshed.
Our helper functions will handle callingauthenticatebefore making any calls, so if you are only using our
helper functions you won't need to call it yourself.Thesessionshould generally be created once per application. It will automatically dispose itself when the application
shuts down. Alternatively you can enclose it in anasync withor awithblock if you need to create multiple sessions,
as shown in the examples below.If you are using the OpenAPI generated code then you can pass thesession.async_clientorsession.sync_clientinto the OpenAPI
generated API client instance as theapi_clientparameter as shown below. Passing inasync_clientwill cause it to useasyncio, and you will need toawaitthe calls. Passing insync_clientwill cause the calls to complete synchronously.Our helper functions all useasynciofor efficient parallelisation of downloads, and must therefore be awaited.The following example shows how to create a session and request some output channels from a study using our helper function:importcanopyimportasyncioasyncwithcanopy.Session(client_id='<your_client_id>',username='<your_username>')assession:study_data=awaitcanopy.load_study(session,'<study_id>','DynamicLap',['sRun','vCar'])# Using the OpenAPI generated client directly:study_api=canopy.openapi.StudyApi(session.async_client)job_result=awaitstudy_api.study_get_study_job_metadata(session.authentication.tenant_id,'<study_id>',0)# Using asyncio.ensure_future() to enable us to perform multiple calls in paralleljob_result_task=asyncio.ensure_future(study_api.study_get_study_job_metadata(session.authentication.tenant_id,'<study_id>',0))job_result_2=awaitjob_result_taskWhen running this code you will be prompted for your client secret and your password if
it is the first timesession.authentication.authenticate()has been called for this session instance. Alternatively
you can pass the client secret and password into the Session class (after fetching them from a secure location) to
avoid being prompted.If you can't useasyncioandasync/awaityou can instead instantiate the session object synchronously
and use thecanopy.runmethod when calling our async helper methods.
You can passsession.sync_clientinto the OpenAPI client classes instead ofsession.async_clientto make them
return results synchronously.importcanopywithcanopy.Session(client_id='<your_client_id>',username='<your_username>')assession:# Note we are using canopy.run(..) to force the async method to run synchronously.# This is a wrapper for asyncio.get_event_loop().run_until_complete(..).study_data=canopy.run(canopy.load_study(session,'<study_id>','DynamicLap',['sRun','vCar']))# Using the OpenAPI generated client synchronously by passing in sync_client:study_api=canopy.openapi.StudyApi(session.sync_client)job_result=study_api.study_get_study_job_metadata(session.authentication.tenant_id,'<study_id>',0)# You can still run synchronous OpenAPI client methods asynchronously using threads if you need to:job_result_thread=study_api.study_get_study_job_metadata(session.authentication.tenant_id,'<study_id>',0,async_req=True)job_result_2=job_result_thread.get()Proxy ServersYou can configure your proxy server by passing in aproxyargument to thecanopy.Sessionobject:asyncwithcanopy.Session(authentication_data,proxy=canopy.ProxyConfiguration('http://some.proxy.com','user','pass'))assession:Updating the OpenAPI ClientThis needs to be tidied up, improved, and automated.Additional options can be found here:https://openapi-generator.tech/docs/generators/openapi/e.g. enumUnknownDefaultCase could be useful if the remaining exposed enums change in future.You can use the Dockerfile in this repository to create a docker image to generate the new API stubs:dockerimagebuild-tcanopy-python-gen:1.
dockercontainerrun-i-t--mounttype=bind,src='<path>/<to>/canopy/canopy-python',dst=/canopy/repocanopy-python-gen:1/bin/bashjava-jaropenapi-generator-cli.jargenerate-gpython-legacy-ihttps://api.canopysimulations.com/swagger/v1/swagger.json-o./gen--package-name"canopy.openapi"rm-rrepo/canopy/openapi
rm-rrepo/docs
cp-rgen/canopy/openapirepo/canopy
cp-rgen/docsrepo
cp-rgen/README.mdrepo/OPENAPI_README.mdTo regenerate theasynciofiles execute:rm-rgen
java-jaropenapi-generator-cli.jargenerate-gpython-legacy-ihttps://api.canopysimulations.com/swagger/v1/swagger.json-o./gen--package-name"canopy.openapi"--libraryasyncio
mvgen/canopy/openapigen/canopy/openapi_asyncio
rm-rgen/canopy/openapi_asyncio/api
rm-rgen/canopy/openapi_asyncio/models
rmgen/canopy/openapi_asyncio/configuration.py
rmgen/canopy/openapi_asyncio/exceptions.py
sed-i's/from canopy\.openapi import rest/from canopy.openapi_asyncio import rest/g'gen/canopy/openapi_asyncio/api_client.py
sed-i'/from canopy.*/d'gen/canopy/openapi_asyncio/__init__.py
sed-i'/# import /d'gen/canopy/openapi_asyncio/__init__.pyecho'from canopy.openapi_asyncio.api_client import ApiClient'>>gen/canopy/openapi_asyncio/__init__.py
cp-rgen/canopy/openapi_asynciorepo/canopyNote: Theopenapi/configuration.pyfile will need to be manually modified to add the default API host URL.
Note: Theopenapi_asyncio/rest.pyfile will need to be manually modified to support proxy servers after generation.
Note: Theopenapi_asyncio/client_api.pyandopenapi/client_api.pyfiles will need to be manually modified to
support numpy array serialization after generation.Documentation for OpenAPI Generated ClientOpenAPI generated documentation can be foundhere.