Python library for the Kloudless API
Python library for the Kloudless API.
To install, clone the repository and run:
python setup.py install
You may also install via pip:
pip install https://github.com/kloudless/kloudless-python/archive/master.zip
Configure Kloudless using the configure method on the kloudless module:
>>> import kloudless >>> kloudless.configure(token="BEARER TOKEN") >>> # Or: >>> kloudless.configure(api_key="API_KEY")
Here are the configuration options:
Here are some of the popular resource classes available:
A full list can be viewed in resources.py.
Each class has the following methods where applicable:
In addition, instances have the methods below where applicable:
Parameters mentioned above:
The Account model has some helper attributes to make using class methods easier. For example:
A full list can be viewed under the Account class in resources.py.
Here is an example retrieving metadata on a folder in an account:
>>> import kloudless; kloudless.configure(api_key="API_KEY") >>> accounts = kloudless.Account.all() >>> account = accounts >>> root_folder = account.folders() >>> children = root_folder.contents() >>> child_folder = [f for f in children if f.type == 'folder']
The shortcut method account.folders is used above, but you can also instantiate the classes above independently:
>>> root_folder = kloudless.Folder(id='root', parent_resource=account)
There are different ways to retrieve information on a resource, given it’s ID. Here are some examples, given account_id and child_folder_id as the account and folder IDs respectively.
# We need to create the account object with an account ID first. >>> account = kloudless.Account(id=account_id) # Get the child folder via the "account.folders" helper method. >>> account.folders.retrieve(id=child_folder_id) # Retrieve the child folder a different way >>> kloudless.Folder.retrieve(id=child_folder.id, parent_resource=account) # Retrieve the child folder another way >>> f = kloudless.Folder(id=child_folder.id, parent_resource=account) >>> f.refresh() # Pulls latest metadata given the ID.
Another example retrieving link information a few different ways:
# A few different ways >>> link = kloudless.Link.all(parent_resource=account) >>> link = account.links.retrieve(id=link.id) >>> link = kloudless.Link.retrieve(id=link.id, parent_resource=account) >>> link = kloudless.Link(id=link.id); link.refresh();
Here’s an example moving a file from one account to a folder in a different account.
# Get two cloud storage accounts. >>> accounts = kloudless.Account.all(); >>> accounts.id 10 >>> accounts.id 20 # Find a file in the first account. >>> root_contents = accounts.folders().contents() # Get the root folder contents >>> f = [f for f in root_contents if f.type == 'file'] # Get a file # Find a folder in the second account. >>> root_contents = accounts.folders().contents() >>> folder = [folder for folder in root_contents if folder.type == 'folder'] # Update the file with new information >>> f.account = accounts.id # Moving it to a different account >>> f.name = 'new file name.txt' >>> f.parent_id = folder.id >>> f.save() # Makes the request to move the file. # 'f' now represents the new file object.
To install test dependencies, run pip install -r tests/requirements.txt.
The tests are written using py.test and can be run like so (from within this directory):
pip install tox tox
Integration tests are seperate and can be run collectively or independently within the tests/integration/ directory, with an appropriate API_KEY set in the environment. Here is a full list of environment variables that can be used to configure the tests:
API_KEY: Required. The API Key to use.
DEV_KEY: Required if testing the Management API. The Developer Key to use.
BASE_URL: Optional. Defaults to ‘https://api.kloudless.com’. Configures the base URL to use for tests.
SERVICES: Optional. A comma-separated list of service names to restrict the services tested.
ACCOUNTS: Optional. A comma-separated list of account IDs to restrict the accounts tested.
REQUESTS_CA_BUNDLE: Optional. If pointing to a BASE_URL secured with a non-trusted root CA certificate, this environment variable can be pointed to the certificate to trust. See http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification for more information.
RUN_LONG_TESTS: Optional. Defaults to a Falsey value. If Truthy, tests which take an extended period of time to run (due to waiting/sleeping), will be included.
API_KEY='...' python test.py API_KEY='...' python test_cases/test_link.py API_KEY='...' SERVICES='dropbox' python test_cases/test_link.py API_KEY='...' SERVICES='dropbox,s3,box' python test.py DEV_KEY='...' BASE_URL='...' python management_api/test_application.py API_KEY='...' DEV_KEY='...' BASE_URL='...' python test.py
An account for each service will be obtained from the API to run tests for.