FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cloudconvert-dotnet/CloudConvert.API/CloudConvertAPI.cs at master · cloudconvert/cloudconvert-dotnet · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
cloudconvert
/
cloudconvert-dotnet
Public
Notifications
You must be signed in to change notification settings
Fork
19
Star
23
Code
Issues
6
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cloudconvert-dotnet
/
CloudConvert.API
/
CloudConvertAPI.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
300 lines (255 loc) · 14.2 KB
Breadcrumbs
cloudconvert-dotnet
/
CloudConvert.API
/
CloudConvertAPI.cs
Copy path
File metadata and controls
300 lines (255 loc) · 14.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
IO
;
using
System
.
Linq
;
using
System
.
Net
.
Http
;
using
System
.
Security
.
Cryptography
;
using
System
.
Text
;
using
System
.
Text
.
Json
;
using
System
.
Threading
;
using
System
.
Threading
.
Tasks
;
using
CloudConvert
.
API
.
Models
;
using
CloudConvert
.
API
.
Models
.
JobModels
;
using
CloudConvert
.
API
.
Models
.
TaskModels
;
namespace
CloudConvert
.
API
{
public
interface
ICloudConvertAPI
{
#region Jobs
Task
<
ListResponse
<
JobResponse
>
>
GetAllJobsAsync
(
JobListFilter
jobFilter
,
CancellationToken
cancellationToken
=
default
)
;
Task
<
Response
<
JobResponse
>
>
CreateJobAsync
(
JobCreateRequest
request
,
CancellationToken
cancellationToken
=
default
)
;
Task
<
Response
<
JobResponse
>
>
GetJobAsync
(
string
id
,
CancellationToken
cancellationToken
=
default
)
;
Task
<
Response
<
JobResponse
>
>
WaitJobAsync
(
string
id
,
CancellationToken
cancellationToken
=
default
)
;
Task
DeleteJobAsync
(
string
id
,
CancellationToken
cancellationToken
=
default
)
;
#endregion
#region Tasks
Task
<
ListResponse
<
TaskResponse
>
>
GetAllTasksAsync
(
TaskListFilter
jobFilter
,
CancellationToken
cancellationToken
=
default
)
;
Task
<
Response
<
TaskResponse
>
>
CreateTaskAsync
<
T
>
(
string
operation
,
T
request
,
CancellationToken
cancellationToken
=
default
)
;
Task
<
Response
<
TaskResponse
>
>
GetTaskAsync
(
string
id
,
string
include
=
null
,
CancellationToken
cancellationToken
=
default
)
;
Task
<
Response
<
TaskResponse
>
>
WaitTaskAsync
(
string
id
,
CancellationToken
cancellationToken
=
default
)
;
Task
DeleteTaskAsync
(
string
id
,
CancellationToken
cancellationToken
=
default
)
;
#endregion
Task
<
string
>
UploadAsync
(
string
url
,
byte
[
]
file
,
string
fileName
,
object
parameters
,
CancellationToken
cancellationToken
=
default
)
;
Task
<
string
>
UploadAsync
(
string
url
,
Stream
file
,
string
fileName
,
object
parameters
,
CancellationToken
cancellationToken
=
default
)
;
bool
ValidateWebhookSignatures
(
string
payloadString
,
string
signature
,
string
signingSecret
)
;
string
CreateSignedUrl
(
string
baseUrl
,
string
signingSecret
,
JobCreateRequest
job
,
string
cacheKey
=
null
)
;
}
public
class
CloudConvertAPI
:
ICloudConvertAPI
{
readonly
string
_apiUrl
;
readonly
string
_apiSyncUrl
;
readonly
RestHelper
_restHelper
;
readonly
string
_api_key
=
"Bearer "
;
private
const
string
SandboxUrlApi
=
"https://api.sandbox.cloudconvert.com/v2"
;
private
const
string
PublicUrlApi
=
"https://api.cloudconvert.com/v2"
;
private
const
string
SandboxUrlSyncApi
=
"https://sync.api.sandbox.cloudconvert.com/v2"
;
private
const
string
PublicUrlSyncApi
=
"https://sync.api.cloudconvert.com/v2"
;
static
readonly
char
[
]
base64Padding
=
{
'='
}
;
internal
CloudConvertAPI
(
RestHelper
restHelper
,
string
api_key
,
bool
isSandbox
=
false
)
{
_apiUrl
=
isSandbox
?
SandboxUrlApi
:
PublicUrlApi
;
_apiSyncUrl
=
isSandbox
?
SandboxUrlSyncApi
:
PublicUrlSyncApi
;
_api_key
=
$
"Bearer
{
api_key
}
"
;
_restHelper
=
restHelper
;
}
public
CloudConvertAPI
(
string
api_key
,
bool
isSandbox
=
false
)
:
this
(
new
RestHelper
(
)
,
api_key
,
isSandbox
)
{
}
public
CloudConvertAPI
(
string
url
,
string
api_key
)
{
_apiUrl
=
url
;
_api_key
=
$
"Bearer
{
api_key
}
"
;
_restHelper
=
new
RestHelper
(
)
;
}
private
HttpRequestMessage
GetRequest
(
string
endpoint
,
HttpMethod
method
,
object
model
=
null
)
{
var
request
=
new
HttpRequestMessage
{
RequestUri
=
new
Uri
(
endpoint
)
,
Method
=
method
}
;
if
(
model
is
not
null
)
{
var
content
=
new
StringContent
(
JsonSerializer
.
Serialize
(
model
,
DefaultJsonSerializerOptions
.
SerializerOptions
)
,
Encoding
.
UTF8
,
"application/json"
)
;
request
.
Content
=
content
;
}
request
.
Headers
.
Add
(
"Authorization"
,
_api_key
)
;
request
.
Headers
.
Add
(
"User-Agent"
,
"cloudconvert-dotnet/v"
+
System
.
Reflection
.
Assembly
.
GetExecutingAssembly
(
)
.
GetName
(
)
.
Version
.
ToString
(
)
+
" (https://github.com/cloudconvert/cloudconvert-dotnet)"
)
;
return
request
;
}
private
HttpRequestMessage
GetMultipartFormDataRequest
(
string
endpoint
,
HttpMethod
method
,
HttpContent
fileContent
,
string
fileName
,
Dictionary
<
string
,
string
>
parameters
=
null
)
{
var
content
=
new
MultipartFormDataContent
(
)
;
var
request
=
new
HttpRequestMessage
{
RequestUri
=
new
Uri
(
endpoint
)
,
Method
=
method
,
}
;
if
(
parameters
is
not
null
)
{
foreach
(
var
param
in
parameters
)
{
content
.
Add
(
new
StringContent
(
param
.
Value
)
,
param
.
Key
)
;
}
}
fileContent
.
Headers
.
Add
(
"Content-Disposition"
,
$
"form-data; name=
\"
file
\"
; filename=
\"
{
new
string
(
Encoding
.
UTF8
.
GetBytes
(
fileName
)
.
Select
(
b
=>
(
char
)
b
)
.
ToArray
(
)
)
}
\"
"
)
;
content
.
Add
(
fileContent
)
;
request
.
Content
=
content
;
return
request
;
}
#region Jobs
/// <summary>
/// List all jobs. Requires the task.read scope.
/// </summary>
/// <param name="jobFilter"></param>
/// <param name="cancellationToken"></param>
/// <returns>
/// The list of jobs. You can find details about the job model response in the documentation about the show jobs endpoint.
/// </returns>
public
Task
<
ListResponse
<
JobResponse
>
>
GetAllJobsAsync
(
JobListFilter
jobFilter
,
CancellationToken
cancellationToken
=
default
)
=>
_restHelper
.
RequestAsync
<
ListResponse
<
JobResponse
>
>
(
GetRequest
(
$
"
{
_apiUrl
}
/jobs?filter[status]=
{
jobFilter
.
Status
}
&filter[tag]=
{
jobFilter
.
Tag
}
&include=
{
jobFilter
.
Include
}
&per_page=
{
jobFilter
.
PerPage
}
&page=
{
jobFilter
.
Page
}
"
,
HttpMethod
.
Get
)
,
cancellationToken
)
;
/// <summary>
/// Create a job with one ore more tasks. Requires the task.write scope.
/// </summary>
/// <param name="model"></param>
/// <param name="cancellationToken"></param>
/// <returns>
/// The created job. You can find details about the job model response in the documentation about the show jobs endpoint.
/// </returns>
public
Task
<
Response
<
JobResponse
>
>
CreateJobAsync
(
JobCreateRequest
model
,
CancellationToken
cancellationToken
=
default
)
=>
_restHelper
.
RequestAsync
<
Response
<
JobResponse
>
>
(
GetRequest
(
$
"
{
_apiUrl
}
/jobs"
,
HttpMethod
.
Post
,
model
)
,
cancellationToken
)
;
/// <summary>
/// Show a job. Requires the task.read scope.
/// </summary>
/// <param name="id"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public
Task
<
Response
<
JobResponse
>
>
GetJobAsync
(
string
id
,
CancellationToken
cancellationToken
=
default
)
=>
_restHelper
.
RequestAsync
<
Response
<
JobResponse
>
>
(
GetRequest
(
$
"
{
_apiUrl
}
/jobs/
{
id
}
"
,
HttpMethod
.
Get
)
,
cancellationToken
)
;
/// <summary>
/// Wait until the job status is finished or error. This makes the request block until the job has been completed. Requires the task.read scope.
///
/// We do not recommend using this for long running jobs (e.g. video encodings).
/// Your system might automatically time out requests if there is not data transferred for a longer time.
/// In general, please avoid to block your application until a CloudConvert job completes.
/// There might be cases in which we need to queue your job which results in longer processing times than usual.
/// Using an asynchronous approach with webhooks is beneficial in such cases.
/// </summary>
/// <param name="id"></param>
/// <param name="cancellationToken"></param>
/// <returns>
/// The finished or failed job, including tasks. You can find details about the job model response in the documentation about the show job endpoint.
/// </returns>
public
Task
<
Response
<
JobResponse
>
>
WaitJobAsync
(
string
id
,
CancellationToken
cancellationToken
=
default
)
=>
_restHelper
.
RequestAsync
<
Response
<
JobResponse
>
>
(
GetRequest
(
$
"
{
_apiSyncUrl
}
/jobs/
{
id
}
"
,
HttpMethod
.
Get
)
,
cancellationToken
)
;
/// <summary>
/// Delete a job, including all tasks and data. Requires the task.write scope.
/// Jobs are deleted automatically 24 hours after they have ended.
/// </summary>
/// <param name="id"></param>
/// <param name="cancellationToken"></param>
/// <returns>
/// An empty response with HTTP Code 204.
/// </returns>
public
Task
DeleteJobAsync
(
string
id
,
CancellationToken
cancellationToken
=
default
)
=>
_restHelper
.
RequestAsync
<
object
>
(
GetRequest
(
$
"
{
_apiUrl
}
/jobs/
{
id
}
"
,
HttpMethod
.
Delete
)
,
cancellationToken
)
;
#endregion
#region Tasks
/// <summary>
/// List all tasks with their status, payload and result. Requires the task.read scope.
/// </summary>
/// <param name="taskFilter"></param>
/// <param name="cancellationToken"></param>
/// <returns>
/// The list of tasks. You can find details about the task model response in the documentation about the show tasks endpoint.
/// </returns>
public
Task
<
ListResponse
<
TaskResponse
>
>
GetAllTasksAsync
(
TaskListFilter
taskFilter
,
CancellationToken
cancellationToken
=
default
)
=>
_restHelper
.
RequestAsync
<
ListResponse
<
TaskResponse
>
>
(
GetRequest
(
$
"
{
_apiUrl
}
/tasks?filter[job_id]=
{
taskFilter
.
JobId
}
&filter[status]=
{
taskFilter
.
Status
}
&filter[operation]=
{
taskFilter
.
Operation
}
&include=
{
taskFilter
.
Include
}
&per_page=
{
taskFilter
.
PerPage
}
&page=
{
taskFilter
.
Page
}
"
,
HttpMethod
.
Get
)
,
cancellationToken
)
;
/// <summary>
/// Create task.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
/// <param name="cancellationToken"></param>
/// <returns>
/// The created task. You can find details about the task model response in the documentation about the show tasks endpoint.
/// </returns>
public
Task
<
Response
<
TaskResponse
>
>
CreateTaskAsync
<
T
>
(
string
operation
,
T
model
,
CancellationToken
cancellationToken
=
default
)
=>
_restHelper
.
RequestAsync
<
Response
<
TaskResponse
>
>
(
GetRequest
(
$
"
{
_apiUrl
}
/
{
operation
}
"
,
HttpMethod
.
Post
,
model
)
,
cancellationToken
)
;
/// <summary>
/// Show a task. Requires the task.read scope.
/// </summary>
/// <param name="id"></param>
/// <param name="include"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public
Task
<
Response
<
TaskResponse
>
>
GetTaskAsync
(
string
id
,
string
include
=
null
,
CancellationToken
cancellationToken
=
default
)
=>
_restHelper
.
RequestAsync
<
Response
<
TaskResponse
>
>
(
GetRequest
(
$
"
{
_apiUrl
}
/tasks/
{
id
}
?include=
{
include
}
"
,
HttpMethod
.
Get
)
,
cancellationToken
)
;
/// <summary>
/// Wait until the task status is finished or error. This makes the request block until the task has been completed. Requires the task.read scope.
///
/// We do not recommend using this for long running jobs (e.g. video encodings).
/// Your system might automatically time out requests if there is not data transferred for a longer time.
/// In general, please avoid to block your application until a CloudConvert job completes.
/// There might be cases in which we need to queue your task which results in longer processing times than usual.
/// Using an asynchronous approach with webhooks is beneficial in such cases.
/// </summary>
/// <param name="id"></param>
/// <param name="cancellationToken"></param>
/// <returns>
/// The finished or failed task. You can find details about the task model response in the documentation about the show tasks endpoint.
/// </returns>
public
Task
<
Response
<
TaskResponse
>
>
WaitTaskAsync
(
string
id
,
CancellationToken
cancellationToken
=
default
)
=>
_restHelper
.
RequestAsync
<
Response
<
TaskResponse
>
>
(
GetRequest
(
$
"
{
_apiSyncUrl
}
/tasks/
{
id
}
"
,
HttpMethod
.
Get
)
,
cancellationToken
)
;
/// <summary>
/// Delete a task, including all data. Requires the task.write scope.
/// Tasks are deleted automatically 24 hours after they have ended.
/// </summary>
/// <param name="id"></param>
/// <param name="cancellationToken"></param>
/// <returns>
/// An empty response with HTTP Code 204.
/// </returns>
public
Task
DeleteTaskAsync
(
string
id
,
CancellationToken
cancellationToken
=
default
)
=>
_restHelper
.
RequestAsync
<
object
>
(
GetRequest
(
$
"
{
_apiUrl
}
/tasks/
{
id
}
"
,
HttpMethod
.
Delete
)
,
cancellationToken
)
;
#endregion
public
Task
<
string
>
UploadAsync
(
string
url
,
byte
[
]
file
,
string
fileName
,
object
parameters
,
CancellationToken
cancellationToken
)
=>
_restHelper
.
RequestAsync
(
GetMultipartFormDataRequest
(
url
,
HttpMethod
.
Post
,
new
ByteArrayContent
(
file
)
,
fileName
,
GetParameters
(
parameters
)
)
,
cancellationToken
)
;
public
Task
<
string
>
UploadAsync
(
string
url
,
Stream
stream
,
string
fileName
,
object
parameters
,
CancellationToken
cancellationToken
=
default
)
=>
_restHelper
.
RequestAsync
(
GetMultipartFormDataRequest
(
url
,
HttpMethod
.
Post
,
new
StreamContent
(
stream
)
,
fileName
,
GetParameters
(
parameters
)
)
,
cancellationToken
)
;
public
string
CreateSignedUrl
(
string
baseUrl
,
string
signingSecret
,
JobCreateRequest
job
,
string
cacheKey
=
null
)
{
var
jobJson
=
JsonSerializer
.
Serialize
(
job
,
DefaultJsonSerializerOptions
.
SerializerOptions
)
;
var
base64Job
=
Convert
.
ToBase64String
(
Encoding
.
ASCII
.
GetBytes
(
jobJson
)
)
.
TrimEnd
(
base64Padding
)
.
Replace
(
'+'
,
'-'
)
.
Replace
(
'/'
,
'_'
)
;
var
builder
=
new
StringBuilder
(
baseUrl
)
.
Append
(
"?job="
)
.
Append
(
base64Job
)
;
if
(
cacheKey
is
not
null
)
{
builder
.
Append
(
"&cache_key="
)
.
Append
(
cacheKey
)
;
}
var
urlWithoutSignature
=
builder
.
ToString
(
)
;
var
signature
=
HashHMAC
(
signingSecret
,
urlWithoutSignature
)
;
return
builder
.
Append
(
"&s="
)
.
Append
(
signature
)
.
ToString
(
)
;
}
public
bool
ValidateWebhookSignatures
(
string
payloadString
,
string
signature
,
string
signingSecret
)
{
string
hashHMAC
=
HashHMAC
(
signingSecret
,
payloadString
)
;
return
hashHMAC
==
signature
;
}
private
static
string
HashHMAC
(
string
key
,
string
message
)
{
using
var
hmac
=
new
HMACSHA256
(
Encoding
.
UTF8
.
GetBytes
(
key
)
)
;
var
hash
=
hmac
.
ComputeHash
(
Encoding
.
UTF8
.
GetBytes
(
message
)
)
;
return
BitConverter
.
ToString
(
hash
)
.
Replace
(
"-"
,
""
)
.
ToLower
(
)
;
}
private
Dictionary
<
string
,
string
>
GetParameters
(
object
parameters
)
{
var
dictionaryParameters
=
new
Dictionary
<
string
,
string
>
(
)
;
if
(
parameters
is
JsonElement
jsonElement
)
{
foreach
(
JsonProperty
prop
in
jsonElement
.
EnumerateObject
(
)
)
{
dictionaryParameters
[
prop
.
Name
]
=
prop
.
Value
.
GetString
(
)
;
}
}
return
dictionaryParameters
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL