Skip to content

Commit

Permalink
Add various improvements to docs (#3)
Browse files Browse the repository at this point in the history
* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md
  • Loading branch information
KTibow authored Mar 27, 2020
1 parent f208cc9 commit 05a8df8
Showing 1 changed file with 45 additions and 15 deletions.
60 changes: 45 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This fork is maintained by Atakama, LLC. This is not the official sdk but a nu
- OrderedDict fix for python 3.5 (https://github.com/OneDrive/onedrive-sdk-python/pull/116)
- Socket linger fix (https://github.com/OneDrive/onedrive-sdk-python/pull/96)
- PyInstaller packaging fix
- Improve docs (https://github.com/OneDrive/onedrive-sdk-python/pull/174)

If you are looking for an alternative api, consider using one of:

Expand All @@ -32,9 +33,9 @@ Next, include the SDK in your Python project by adding:

### OneDrive

To interact with the OneDrive API, your app must authenticate. You can use the following code sample to do so.
To interact with the OneDrive API, your app must authenticate with a client ID and client secret (available at [Azure](https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade)). You can use the following code sample to do so.

```python
```python3
import onedrivesdk

redirect_uri = 'http://localhost:8080/'
Expand All @@ -55,7 +56,7 @@ auth_url = client.auth_provider.get_auth_url(redirect_uri)
print('Paste this URL into your browser, approve the app\'s access.')
print('Copy everything in the address bar after "code=", and paste it below.')
print(auth_url)
code = raw_input('Paste code here: ')
code = input('Paste code here: ')

client.auth_provider.authenticate(code, redirect_uri, client_secret)
```
Expand All @@ -64,7 +65,7 @@ The above code requires copy-pasting into your browser and back into your consol
that manual work, you can use the helper class `GetAuthCodeServer`. That helper class spins up a webserver, so
this method cannot be used on all environments.

```python
```python3
import onedrivesdk
from onedrivesdk.helpers import GetAuthCodeServer

Expand Down Expand Up @@ -94,7 +95,7 @@ Then, you can build a client to access those resources. This uses a slightly dif
auth flow than the standard code flow - note the use of `redeem_refresh_token` with
the `service_resource_id` of the service you want to access.

```python
```python3
import onedrivesdk
from onedrivesdk.helpers import GetAuthCodeServer
from onedrivesdk.helpers.resource_discovery import ResourceDiscoveryRequest
Expand Down Expand Up @@ -128,13 +129,19 @@ client = onedrivesdk.OneDriveClient(service_info.service_resource_id + '/_api/v2

### Upload an Item

```python
```python3
returned_item = client.item(drive='me', id='root').children['newfile.txt'].upload('./path_to_file.txt')
```

### Select an Item by Filename

```python3
item = client.item(drive='me', path="online_file_name")
```

### Download an Item

```python
```python3
root_folder = client.item(drive='me', id='root').children.get()
id_of_file = root_folder[0].id

Expand All @@ -143,7 +150,7 @@ client.item(drive='me', id=id_of_file).download('./path_to_download_to.txt')

### Add a folder

```python
```python3
f = onedrivesdk.Folder()
i = onedrivesdk.Item()
i.name = 'New Folder'
Expand All @@ -154,7 +161,7 @@ returned_item = client.item(drive='me', id='root').children.add(i)

### Copy an Item

```python
```python3
from onedrivesdk.item_reference import ItemReference

ref = ItemReference()
Expand All @@ -171,17 +178,26 @@ copy_operation.poll_until_complete()

### Rename an Item

```python
```python3
renamed_item = onedrivesdk.Item()
renamed_item.name = 'NewItemName'
renamed_item.id = 'youritemtorename!id'

new_item = client.item(drive='me', id=renamed_item.id).update(renamed_item)
```

### Delete an Item
#### Warning: Make sure you really want to do this.

```python3
root_folder = client.item(drive='me', id='root').children.get()
id_of_file = root_folder[0].id
# WARNING: This can have catastrophic consequences. Make sure you really want to do this.
client.item(drive='me', id=id_of_file).delete()
```
### Paging through a collection

```python
```python3
#get the top three elements of root, leaving the next page for more elements
collection = client.item(drive='me', id='root').children.request(top=3).get()

Expand All @@ -197,8 +213,10 @@ collection2 = onedrivesdk.ChildrenCollectionRequest.get_next_page_request(collec
For async operations, you create an `asyncio.coroutine` which
implements `asyncio.ascompleted`, and execute it with
`loop.run\_until\_complete`.
To download an item, use `download_async()` instead of `download()`.
To upload an item, use `upload_async()` instead of `upload()`.

```python
```python3
import asyncio

@asyncio.coroutine
Expand All @@ -211,13 +229,22 @@ def run_gets(client):
loop = asyncio.get_event_loop()
loop.run_until_complete(run_gets(client))
```
#### Upload Progress

For async uploads, you can get the progress by calling `upload_async` with a
`function(current_part, total_parts)` to call every time a 10MB chunk is uploaded.
```python3
def status(current_part, total_parts):
print(str(current_part)+"/"+str(total_parts))
returned_item = client.item(dris:open ive='me', id='root').children['newfile.txt'].upload_async('./path_to_file.txt', upload_status=status)
```

## Saving and Loading a Session

You can save your OAuth session details so that you don't have to go through the full
OAuth flow every time you start your app. To do so, follow these steps:

```python
```python3
auth_provider = onedrivesdk.AuthProvider(http_provider,
client_id,
scopes)
Expand All @@ -232,7 +259,7 @@ auth_provider = onedrivesdk.AuthProvider(http_provider,
scopes)
auth_provider.load_session()
auth_provider.refresh_token()
client = onedrivesdk.OneDriveClient(base_url, auth_provider, http_provider)
client = onedrivesdk.OneDriveClient(api_base_url, auth_provider, http_provider)
```

After the call to `refresh_token()` your `AuthProvider` will be ready to authenticate calls
Expand All @@ -247,9 +274,12 @@ of `Session`. For example, the default implementation tries to open the file `se
which may not exist and will raise `FileNotFoundError`. You will need to account for that here
(or, even better, in your implementation of `Session`).

## CLI
If you'd rather use a command line, then you can use the [example](https://github.com/OneDrive/onedrive-sdk-python/tree/master/examples) instead.

## Using a Proxy
If you need to proxy your requests, you can use the helper class `HttpProviderWithProxy`.
```python
```python3
import onedrivesdk
from onedrivesdk.helpers import http_provider_with_proxy

Expand Down

0 comments on commit 05a8df8

Please sign in to comment.