Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Examples #28

Open
JimBobSquarePants opened this issue Jul 4, 2016 · 10 comments
Open

Better Examples #28

JimBobSquarePants opened this issue Jul 4, 2016 · 10 comments

Comments

@JimBobSquarePants
Copy link

Hi there,

The example code in the repository seems sorely limited.

Two use-cases I can't seem to find for example, are uploaded files in chunks and retrieving the share url for a file once it has been uploaded.

Would it be possible to add example code demonstrating how to do both of those things?

Cheers!

@greg-db
Copy link
Contributor

greg-db commented Jul 5, 2016

@JimBobSquarePants thanks for the feedback! I'm sending this along to the team as a request to expand on the samples for these use cases.

Here's some basic sample code I have handy for uploading files, using upload sessions for larger files:

private async Task Upload(string localPath, string remotePath)
{
    const int ChunkSize = 4096 * 1024;
    using (var fileStream = File.Open(localPath, FileMode.Open))
    {
        if (fileStream.Length <= ChunkSize)
        {
            await this.client.Files.UploadAsync(remotePath, body: fileStream);
        }
        else
        {
            await this.ChunkUpload(remotePath, fileStream, ChunkSize);
        }
    }
}

private async Task ChunkUpload(String path, FileStream stream, int chunkSize)
{
    int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSize);
    byte[] buffer = new byte[chunkSize];
    string sessionId = null;
    for (var idx = 0; idx < numChunks; idx++)
    {
        var byteRead = stream.Read(buffer, 0, chunkSize);

        using (var memStream = new MemoryStream(buffer, 0, byteRead))
        {
            if (idx == 0)
            {
                var result = await this.client.Files.UploadSessionStartAsync(false, memStream);
                sessionId = result.SessionId;
            }
            else
            {
                var cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));

                if (idx == numChunks - 1)
                {
                    FileMetadata fileMetadata = await this.client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(path), memStream);
                    Console.WriteLine (fileMetadata.PathDisplay);
                }
                else
                {
                    await this.client.Files.UploadSessionAppendV2Async(cursor, false, memStream);
                }
            }
        }
    }
}

And here's a basic example of getting a shared link for a file, either by creating a new one, or retrieving an existing one:

SharedLinkMetadata sharedLinkMetadata;
try {
    sharedLinkMetadata = await this.client.Sharing.CreateSharedLinkWithSettingsAsync (path);
} catch (ApiException<CreateSharedLinkWithSettingsError> err) {
    if (err.ErrorResponse.IsSharedLinkAlreadyExists) {
        var sharedLinksMetadata = await this.client.Sharing.ListSharedLinksAsync (path, null, true);
        sharedLinkMetadata = sharedLinksMetadata.Links.First();
    } else {
        throw err;
    }
}
Console.WriteLine (sharedLinkMetadata.Url);

@JimBobSquarePants
Copy link
Author

Hi @greg-db Thanks for that. I wasn't aware that the error handling code was necessary for share links so this is most useful!

@greg-db
Copy link
Contributor

greg-db commented Jul 6, 2016

No problem. There are other errors too, but exactly what errors you handle specifically and how is up to you of course. I thought that particular error is relatively important though, since CreateSharedLinkWithSettingsAsync won't just return an existing link. (That's because the existing link won't necessarily have the same settings as you requested, so if that's important to your app, make sure you check that in the result of ListSharedLinksAsync.)

@JimBobSquarePants
Copy link
Author

Will do. Thanks!

@gtsiros
Copy link

gtsiros commented Mar 12, 2018

just wanted to say that better examples should not be considered "enhancement"

decent examples in API documentation is probably the most important part, especially regarding the documentation about it.

it shows how the api is expected to be used, by the programmers that wrote it

@greg-db
Copy link
Contributor

greg-db commented Mar 12, 2018

Thanks for the feedback @gtsiros ! I'll send it along to the team.

@deRightDirection
Copy link

can there be an example added how to login to dropbox from an uwp-app?

my code till so far logs in but stops when it shows the access token :-(

@greg-db
Copy link
Contributor

greg-db commented Sep 6, 2018

@MannusEtten I'll send this along as a request for a UWP example in particular. Thanks!

@deRightDirection
Copy link

deRightDirection commented Sep 6, 2018

thanks @greg-db, i do really wonder how the hell to let users authorize / authenticate with dropbox by oauth. my oauth-screen stops when it shows the accesstoken

when can i expect something to try?

@greg-db
Copy link
Contributor

greg-db commented Sep 7, 2018

@MannusEtten Please open a new issue with details on what you're stuck on or what isn't working, and we'll take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants