55using System . Net . Http ;
66using System . Security . Cryptography ;
77using System . Text ;
8- using System . Threading . Tasks ;
98using System . Text . Json ;
9+ using System . Threading ;
10+ using System . Threading . Tasks ;
11+ using CloudConvert . API . Models ;
1012using CloudConvert . API . Models . JobModels ;
1113using CloudConvert . API . Models . TaskModels ;
12- using CloudConvert . API . Models ;
13- using System . Threading ;
1414
1515namespace CloudConvert . API
1616{
@@ -45,17 +45,17 @@ public class CloudConvertAPI : ICloudConvertAPI
4545
4646 readonly RestHelper _restHelper ;
4747 readonly string _api_key = "Bearer " ;
48- const string sandboxUrlApi = "https://api.sandbox.cloudconvert.com/v2" ;
49- const string publicUrlApi = "https://api.cloudconvert.com/v2" ;
50- const string sandboxUrlSyncApi = "https://sync.api.sandbox.cloudconvert.com/v2" ;
51- const string publicUrlSyncApi = "https://sync.api.cloudconvert.com/v2" ;
48+ private const string SandboxUrlApi = "https://api.sandbox.cloudconvert.com/v2" ;
49+ private const string PublicUrlApi = "https://api.cloudconvert.com/v2" ;
50+ private const string SandboxUrlSyncApi = "https://sync.api.sandbox.cloudconvert.com/v2" ;
51+ private const string PublicUrlSyncApi = "https://sync.api.cloudconvert.com/v2" ;
5252 static readonly char [ ] base64Padding = { '=' } ;
5353
5454 internal CloudConvertAPI ( RestHelper restHelper , string api_key , bool isSandbox = false )
5555 {
56- _apiUrl = isSandbox ? sandboxUrlApi : publicUrlApi ;
57- _apiSyncUrl = isSandbox ? sandboxUrlSyncApi : publicUrlSyncApi ;
58- _api_key += api_key ;
56+ _apiUrl = isSandbox ? SandboxUrlApi : PublicUrlApi ;
57+ _apiSyncUrl = isSandbox ? SandboxUrlSyncApi : PublicUrlSyncApi ;
58+ _api_key = $ "Bearer { api_key } " ;
5959 _restHelper = restHelper ;
6060 }
6161
@@ -67,15 +67,15 @@ public CloudConvertAPI(string api_key, bool isSandbox = false)
6767 public CloudConvertAPI ( string url , string api_key )
6868 {
6969 _apiUrl = url ;
70- _api_key += api_key ;
70+ _api_key = $ "Bearer { api_key } " ;
7171 _restHelper = new RestHelper ( ) ;
7272 }
7373
7474 private HttpRequestMessage GetRequest ( string endpoint , HttpMethod method , object model = null )
7575 {
7676 var request = new HttpRequestMessage { RequestUri = new Uri ( endpoint ) , Method = method } ;
7777
78- if ( model != null )
78+ if ( model is not null )
7979 {
8080 var content = new StringContent ( JsonSerializer . Serialize ( model , DefaultJsonSerializerOptions . SerializerOptions ) , Encoding . UTF8 , "application/json" ) ;
8181 request . Content = content ;
@@ -93,7 +93,7 @@ private HttpRequestMessage GetMultipartFormDataRequest(string endpoint, HttpMeth
9393 var content = new MultipartFormDataContent ( ) ;
9494 var request = new HttpRequestMessage { RequestUri = new Uri ( endpoint ) , Method = method , } ;
9595
96- if ( parameters != null )
96+ if ( parameters is not null )
9797 {
9898 foreach ( var param in parameters )
9999 {
@@ -119,7 +119,7 @@ private HttpRequestMessage GetMultipartFormDataRequest(string endpoint, HttpMeth
119119 /// <returns>
120120 /// The list of jobs. You can find details about the job model response in the documentation about the show jobs endpoint.
121121 /// </returns>
122- public Task < ListResponse < JobResponse > > GetAllJobsAsync ( JobListFilter jobFilter , CancellationToken cancellationToken = default )
122+ public Task < ListResponse < JobResponse > > GetAllJobsAsync ( JobListFilter jobFilter , CancellationToken cancellationToken = default )
123123 => _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 ) ;
124124
125125 /// <summary>
@@ -130,7 +130,7 @@ public Task<ListResponse<JobResponse>> GetAllJobsAsync(JobListFilter jobFilter,
130130 /// <returns>
131131 /// The created job. You can find details about the job model response in the documentation about the show jobs endpoint.
132132 /// </returns>
133- public Task < Response < JobResponse > > CreateJobAsync ( JobCreateRequest model , CancellationToken cancellationToken = default )
133+ public Task < Response < JobResponse > > CreateJobAsync ( JobCreateRequest model , CancellationToken cancellationToken = default )
134134 => _restHelper . RequestAsync < Response < JobResponse > > ( GetRequest ( $ "{ _apiUrl } /jobs", HttpMethod . Post , model ) , cancellationToken ) ;
135135
136136 /// <summary>
@@ -195,7 +195,7 @@ public Task<ListResponse<TaskResponse>> GetAllTasksAsync(TaskListFilter taskFilt
195195 /// <returns>
196196 /// The created task. You can find details about the task model response in the documentation about the show tasks endpoint.
197197 /// </returns>
198- public Task < Response < TaskResponse > > CreateTaskAsync < T > ( string operation , T model , CancellationToken cancellationToken = default )
198+ public Task < Response < TaskResponse > > CreateTaskAsync < T > ( string operation , T model , CancellationToken cancellationToken = default )
199199 => _restHelper . RequestAsync < Response < TaskResponse > > ( GetRequest ( $ "{ _apiUrl } /{ operation } ", HttpMethod . Post , model ) , cancellationToken ) ;
200200
201201 /// <summary>
@@ -205,7 +205,7 @@ public Task<Response<TaskResponse>> CreateTaskAsync<T>(string operation, T model
205205 /// <param name="include"></param>
206206 /// <param name="cancellationToken"></param>
207207 /// <returns></returns>
208- public Task < Response < TaskResponse > > GetTaskAsync ( string id , string include = null , CancellationToken cancellationToken = default )
208+ public Task < Response < TaskResponse > > GetTaskAsync ( string id , string include = null , CancellationToken cancellationToken = default )
209209 => _restHelper . RequestAsync < Response < TaskResponse > > ( GetRequest ( $ "{ _apiUrl } /tasks/{ id } ?include={ include } ", HttpMethod . Get ) , cancellationToken ) ;
210210
211211 /// <summary>
@@ -222,7 +222,7 @@ public Task<Response<TaskResponse>> GetTaskAsync(string id, string include = nul
222222 /// <returns>
223223 /// The finished or failed task. You can find details about the task model response in the documentation about the show tasks endpoint.
224224 /// </returns>
225- public Task < Response < TaskResponse > > WaitTaskAsync ( string id , CancellationToken cancellationToken = default )
225+ public Task < Response < TaskResponse > > WaitTaskAsync ( string id , CancellationToken cancellationToken = default )
226226 => _restHelper . RequestAsync < Response < TaskResponse > > ( GetRequest ( $ "{ _apiSyncUrl } /tasks/{ id } ", HttpMethod . Get ) , cancellationToken ) ;
227227
228228 /// <summary>
@@ -234,34 +234,38 @@ public Task<Response<TaskResponse>> WaitTaskAsync(string id, CancellationToken c
234234 /// <returns>
235235 /// An empty response with HTTP Code 204.
236236 /// </returns>
237- public Task DeleteTaskAsync ( string id , CancellationToken cancellationToken = default )
237+ public Task DeleteTaskAsync ( string id , CancellationToken cancellationToken = default )
238238 => _restHelper . RequestAsync < object > ( GetRequest ( $ "{ _apiUrl } /tasks/{ id } ", HttpMethod . Delete ) , cancellationToken ) ;
239239
240240 #endregion
241241
242- public Task < string > UploadAsync ( string url , byte [ ] file , string fileName , object parameters , CancellationToken cancellationToken )
242+ public Task < string > UploadAsync ( string url , byte [ ] file , string fileName , object parameters , CancellationToken cancellationToken )
243243 => _restHelper . RequestAsync ( GetMultipartFormDataRequest ( url , HttpMethod . Post , new ByteArrayContent ( file ) , fileName , GetParameters ( parameters ) ) , cancellationToken ) ;
244244
245245 public Task < string > UploadAsync ( string url , Stream stream , string fileName , object parameters , CancellationToken cancellationToken = default )
246246 => _restHelper . RequestAsync ( GetMultipartFormDataRequest ( url , HttpMethod . Post , new StreamContent ( stream ) , fileName , GetParameters ( parameters ) ) , cancellationToken ) ;
247247
248248 public string CreateSignedUrl ( string baseUrl , string signingSecret , JobCreateRequest job , string cacheKey = null )
249249 {
250- string url = baseUrl ;
251- string jobJson = JsonSerializer . Serialize ( job , DefaultJsonSerializerOptions . SerializerOptions ) ;
252- string base64Job = System . Convert . ToBase64String ( Encoding . ASCII . GetBytes ( jobJson ) ) . TrimEnd ( base64Padding ) . Replace ( '+' , '-' ) . Replace ( '/' , '_' ) ;
250+ var jobJson = JsonSerializer . Serialize ( job , DefaultJsonSerializerOptions . SerializerOptions ) ;
251+ var base64Job = Convert . ToBase64String ( Encoding . ASCII . GetBytes ( jobJson ) )
252+ . TrimEnd ( base64Padding )
253+ . Replace ( '+' , '-' )
254+ . Replace ( '/' , '_' ) ;
253255
254- url += "?job=" + base64Job ;
256+ var builder = new StringBuilder ( baseUrl )
257+ . Append ( "?job=" )
258+ . Append ( base64Job ) ;
255259
256- if ( cacheKey != null ) {
257- url += "&cache_key=" + cacheKey ;
260+ if ( cacheKey is not null )
261+ {
262+ builder . Append ( "&cache_key=" ) . Append ( cacheKey ) ;
258263 }
259264
260- string signature = HashHMAC ( signingSecret , url ) ;
265+ var urlWithoutSignature = builder . ToString ( ) ;
266+ var signature = HashHMAC ( signingSecret , urlWithoutSignature ) ;
261267
262- url += "&s=" + signature ;
263-
264- return url ;
268+ return builder . Append ( "&s=" ) . Append ( signature ) . ToString ( ) ;
265269 }
266270
267271 public bool ValidateWebhookSignatures ( string payloadString , string signature , string signingSecret )
@@ -271,11 +275,13 @@ public bool ValidateWebhookSignatures(string payloadString, string signature, st
271275 return hashHMAC == signature ;
272276 }
273277
274- private string HashHMAC ( string key , string message )
278+ private static string HashHMAC ( string key , string message )
275279 {
276- byte [ ] hash = new HMACSHA256 ( Encoding . UTF8 . GetBytes ( key ) ) . ComputeHash ( new UTF8Encoding ( ) . GetBytes ( message ) ) ;
280+ using var hmac = new HMACSHA256 ( Encoding . UTF8 . GetBytes ( key ) ) ;
281+ var hash = hmac . ComputeHash ( Encoding . UTF8 . GetBytes ( message ) ) ;
277282 return BitConverter . ToString ( hash ) . Replace ( "-" , "" ) . ToLower ( ) ;
278283 }
284+
279285 private Dictionary < string , string > GetParameters ( object parameters )
280286 {
281287 var dictionaryParameters = new Dictionary < string , string > ( ) ;
0 commit comments