Package: kloudless
Python library for the Kloudless APIPython library for theKloudless API.You need to sign up and create an application first before using this SDK.Table of ContentsRequirementsInstallationGetting StartedFull DocumentationMaking API RequestsIntegrating OAuth FlowRequirementsPython 2.7 or Python 3.5+InstallationInstall via pip:pipinstallkloudlessInstall from source:gitclonegit://github.com/kloudless/kloudless-pythoncdkloudless-python
pythonsetup.pyinstallGetting StartedMost Kloudless API endpoints require connecting to an upstream service
account first. Start bynavigating toAPI Explorerand connecting
an account.After the account has been connected, copy the Bearer Token from the text box
and use it to initialize an Account object:fromkloudlessimportAccountaccount=Account(token="YOUR_BEARER_TOKEN")Full DocumentationFull documentation is hosted atRead the docs.A quick-start is included below.Making API RequestsYou can now make an API request with the account instance you've created.If Connecting to a Storage Service# retrieve folder contentsroot_folder_contents=account.get('storage/folders/root/contents')forresourceinroot_folder_contents.get_paging_iterator():print(resource.data)# download the first file in root_folderforresourceinroot_folder_contents:ifresource.data['type']=='file':filename=resource.data['name']response=resource.get('contents')withopen(filename,'wb')asf:f.write(response.content)break# upload a file to root_folderfile_name='FILE_NAME_TO_UPLOAD'headers={'X-Kloudless-Metadata':json.dumps({'parent_id':'root','name':file_name})}withopen(file_name,'rb')asf:file_resource=account.post('storage/files',data=f,headers=headers)If Connecting to a Calendar Service# retrieve primary calendarcalendar=account.get('cal/calendars/primary')print('Primary Calendar:{}'.format(calendar.data['name']))# iterate through events in first page with page_size equals 5events=calendar.get('events?page_size=5')foreinevents:data=e.dataprint('{}:{}~{}'.format(data['name'],data['start'],data['end']))# iterate thorough events in second pagenext_page_events=events.get_next_page()foreinnext_page_events:data=e.dataprint('{}:{}~{}'.format(data['name'],data['start'],data['end']))# create a new event on primary calendarevent=events.post(json={'start':'2019-01-01T12:30:00Z','end':'2019-01-01T13:30:00Z','name':'Event test'})Integrating OAuth FlowYou can use theAuthenticator JS libraryto authenticate end-users via a pop-up and store the token server-side.
Be sure to verify the token once it is transferred to your
server. Seekloudless.application.verify_token.An alternate approach is to use the OAuth Authorization Code grant flow to
redirect the end-user to Kloudless to connect their account to your app.OAuth Integration Demo serverexamples/demo_server.pyprovides the server-side logic of the 3-legged OAuth
flow using helper methods from the Kloudless Python SDK. Seeexamples/README.mdfor instructions on running the demo server.Python Django Sample codeInsert the following code into Django views underviews/directory and
calling it viaurls.py.fromdjango.httpimportHttpResponseRedirect,HttpResponsefromdjango.confimportsettingsfromkloudlessimportget_authorization_url,get_token_from_codedefstart_authorization_flow(request):"""Redirect the user to start authorization flow."""url,state=get_authorization_url(app_id=settings.KLOUDLESS_APP_ID,redirect_uri=settings.KLOUDLESS_REDIRECT_URL,scope='storage')request.session['authorization_state']=statereturnHttpResponseRedirect(url)defcallback(request):"""The endpoint for settings.KLOUDLESS_REDIRECT_URL."""params=request.GET.dict()token=get_token_from_code(app_id=settings.KLOUDLESS_APP_ID,api_key=settings.KLOUDLESS_API_KEY,orig_state=request.session['authorization_state'],orig_redirect_uri=settings.KLOUDLESS_REDIRECT_URL,**params)# store the tokenrequest.user.kloudless_token=tokenrequest.user.save()returnHttpResponse('Account connects successfully.')