This repository was archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathClient.cs
More file actions
182 lines (152 loc) · 6.25 KB
/
Copy pathClient.cs
File metadata and controls
182 lines (152 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System.Threading;
using DropNetRT.Authentication;
using DropNetRT.Exceptions;
using DropNetRT.HttpHelpers;
using DropNetRT.Models;
using Newtonsoft.Json;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace DropNetRT
{
public partial class DropNetClient
{
private const string ApiBaseUrl = "https://api.dropbox.com";
private const string ApiContentBaseUrl = "https://api-content.dropbox.com";
private const string ApiNotifyBaseUrl = "https://api-notify.dropbox.com";
/// <summary>
/// Do not set this property directly, instead use SetUserToken
/// </summary>
public UserLogin UserLogin { get; set; }
/// <summary>
/// To use Dropbox API in sandbox mode (app folder access) set to true
/// </summary>
public bool UseSandbox { get; set; }
public TimeSpan Timeout
{
get { return _httpClient.Timeout; }
set { _httpClient.Timeout = value; }
}
private const string SandboxRoot = "sandbox";
private const string DropboxRoot = "dropbox";
private readonly string _apiKey;
private readonly string _apisecret;
private HttpMessageHandler _httpHandler;
private OAuthMessageHandler _oauthHandler;
private HttpClient _httpClient;
private IWebProxy _proxy;
private const string _lineBreak = "\r\n";
private const string _formBoundary = "-----------------------------28947758029299";
/// <summary>
/// Gets the directory root for the requests (full or sandbox mode)
/// </summary>
string Root
{
get { return UseSandbox ? SandboxRoot : DropboxRoot; }
}
/// <summary>
/// Default Constructor for the DropboxClient
/// </summary>
/// <param name="apiKey">The Api Key to use for the Dropbox Requests</param>
/// <param name="appSecret">The Api Secret to use for the Dropbox Requests</param>
public DropNetClient(string apiKey, string appSecret)
{
_apiKey = apiKey;
_apisecret = appSecret;
LoadClient();
}
/// <summary>
/// Creates an instance of the DropNetClient given an API Key/Secret and a User Token/Secret
/// </summary>
/// <param name="apiKey">The Api Key to use for the Dropbox Requests</param>
/// <param name="apiSecret">The Api Secret to use for the Dropbox Requests</param>
/// <param name="userToken">The User authentication token</param>
/// <param name="userSecret">The Users matching secret</param>
/// <param name="proxy">The proxy to use for web requests</param>
public DropNetClient(string apiKey, string apiSecret, string userToken, string userSecret, IWebProxy proxy = null)
{
_apiKey = apiKey;
_apisecret = apiSecret;
UserLogin = new UserLogin { Token = userToken, Secret = userSecret };
_proxy = proxy;
LoadClient();
}
/// <summary>
/// Internal method to load up the HttpClient stuff
/// </summary>
private void LoadClient()
{
//Default to full access
UseSandbox = false;
var handler = new HttpClientHandler();
if (_proxy != null)
{
handler.Proxy = _proxy;
handler.UseProxy = true;
}
_httpHandler = handler;
if (UserLogin != null)
{
_oauthHandler = new OAuthMessageHandler(_httpHandler, _apiKey, _apisecret, UserLogin.Token, UserLogin.Secret);
}
else
{
_oauthHandler = new OAuthMessageHandler(_httpHandler, _apiKey, _apisecret);
}
_httpClient = new HttpClient(_oauthHandler);
_httpClient.DefaultRequestHeaders.Add("User-Agent", "DropNetRT");
if (_httpClient.DefaultRequestHeaders.Any(h => h.Key == "Connection"))
{
_httpClient.DefaultRequestHeaders.Remove("Connection");
}
}
enum ApiType
{
Base,
Content,
Notify
}
/// <summary>
/// Wrapper around the HttpClient.SendAsync function with error handling
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task<T> SendAsync<T>(HttpRequest request, CancellationToken cancellationToken) where T : class
{
string responseBody = await SendAsync(request, cancellationToken);
return JsonConvert.DeserializeObject<T>(responseBody);
}
private async Task<string> SendAsync(HttpRequest request, CancellationToken cancellationToken)
{
//Authenticate with oauth
_oauthHandler.Authenticate(request);
HttpResponseMessage response;
try
{
response = await _httpClient.SendAsync(request, cancellationToken);
}
catch (TaskCanceledException ex)
{
// Expiration (cancellation) of the longpoll call is a normal situation, it happens regularly if longpoll set to higher values (480 is the max) and as a part of an expected workflow. Seen from this angle it is not an 'exception'.
// For that reason (and the possibility of a simpler consumer code) we simulate "no changes" reply from the server in this very particular case instead of throwing a generic DropBoxException.
if (request.RequestUri.AbsolutePath.Contains("longpoll"))
return "{\"changes\" : false}";
throw new DropboxException(ex);
}
catch (Exception ex)
{
throw new DropboxException(ex);
}
//TODO - More Error Handling
if (response.StatusCode != HttpStatusCode.OK)
{
throw new DropboxException(response);
}
return await response.Content.ReadAsStringAsync();
}
}
}