Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CloudConvert.API/CloudConvert.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>

Expand Down
65 changes: 49 additions & 16 deletions CloudConvert.API/CloudConvertAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using CloudConvert.API.Models.JobModels;
using CloudConvert.API.Models.TaskModels;
using CloudConvert.API.Models;
Expand Down Expand Up @@ -74,16 +73,17 @@ public CloudConvertAPI(string url, string api_key)
private HttpRequestMessage GetRequest(string endpoint, HttpMethod method, object model = null)
{
var request = new HttpRequestMessage { RequestUri = new Uri(endpoint), Method = method };

if (model != null)
{
var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
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)");
request.Headers.Add("User-Agent", "cloudconvert-dotnet/v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + " (https://github.com/cloudconvert/cloudconvert-dotnet)");

return request;
}

Expand All @@ -100,7 +100,7 @@ private HttpRequestMessage GetMultipartFormDataRequest(string endpoint, HttpMeth
}
}

fileContent.Headers.Add("Content-Disposition", $"form-data; name=\"file\"; filename=\"{ new string(Encoding.UTF8.GetBytes(fileName).Select(b => (char)b).ToArray())}\"");
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;
Expand Down Expand Up @@ -225,10 +225,10 @@ private HttpRequestMessage GetMultipartFormDataRequest(string endpoint, HttpMeth
public string CreateSignedUrl(string baseUrl, string signingSecret, JobCreateRequest job, string cacheKey = null)
{
string url = baseUrl;
string jobJson = JsonConvert.SerializeObject(job);
string jobJson = JsonSerializer.Serialize(job, DefaultJsonSerializerOptions.SerializerOptions);
string base64Job = System.Convert.ToBase64String(Encoding.ASCII.GetBytes(jobJson)).TrimEnd(base64Padding).Replace('+', '-').Replace('/', '_');
url += "?job=" + base64Job;

url += "?job=" + base64Job;

if(cacheKey != null) {
url += "&cache_key=" + cacheKey;
Expand All @@ -254,14 +254,47 @@ private string HashHMAC(string key, string message)
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}

// FIXME
private Dictionary<string, string> GetParameters(object parameters)
{
var attributes = ((JToken)parameters).ToList();
Dictionary<string, string> dictionaryParameters = new Dictionary<string, string>();
foreach (JToken attribute in attributes)
var dictionaryParameters = new Dictionary<string, string>();

if (parameters != null)
{
JProperty jProperty = attribute.ToObject<JProperty>();
dictionaryParameters.Add(jProperty.Name, jProperty.Value.ToString());
// Serialize the input object to JSON.
string json = JsonSerializer.Serialize(parameters, DefaultJsonSerializerOptions.SerializerOptions);

// Parse the JSON using JsonDocument.
using (var doc = JsonDocument.Parse(json))
{
var root = doc.RootElement;
if (root.ValueKind == JsonValueKind.Object)
{
foreach (JsonProperty prop in root.EnumerateObject())
{
string valueStr;
// Replicate JToken.ToString() behavior:
// For string values, return the unquoted string.
// For objects and arrays, serialize them to JSON.
// Otherwise, use the default ToString().
switch (prop.Value.ValueKind)
{
case JsonValueKind.String:
valueStr = prop.Value.GetString();
break;
case JsonValueKind.Object:
case JsonValueKind.Array:
valueStr = JsonSerializer.Serialize(prop.Value, DefaultJsonSerializerOptions.SerializerOptions);
break;
default:
valueStr = prop.Value.ToString();
break;
}

dictionaryParameters[prop.Name] = valueStr;
}
}
}
}

return dictionaryParameters;
Expand Down
14 changes: 14 additions & 0 deletions CloudConvert.API/DefaultJsonSerializerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text.Json.Serialization;
using System.Text.Json;

namespace CloudConvert.API
{
public class DefaultJsonSerializerOptions
{
public static readonly JsonSerializerOptions SerializerOptions = new()
{
Converters = { new JsonStringEnumConverter() },
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
}
}
8 changes: 4 additions & 4 deletions CloudConvert.API/Models/ErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace CloudConvert.API.Models
{
public partial class ErrorResponse
{
[JsonProperty("message")]
[JsonPropertyName("message")]
public string Message { get; set; }

[JsonProperty("code")]
[JsonPropertyName("code")]
public string Code { get; set; }

[JsonProperty("errors")]
[JsonPropertyName("errors")]
public object Errors { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace CloudConvert.API.Models.ExportOperations
{
public class ExportAzureBlobCreateRequest
{
[JsonProperty("operation")]
public static string Operation = "export/azure/blob";
[JsonPropertyName("operation")]
public string Operation { get; } = "export/azure/blob";

/// <summary>
/// The input task name(s) for this task.
/// input: string | string[];
/// </summary>
[JsonProperty("input")]
public dynamic Input { get; set; }
[JsonPropertyName("input")]
public object Input { get; set; }

[JsonProperty("storage_account")]
[JsonPropertyName("storage_account")]
public string Storage_Account { get; set; }

[JsonProperty("storage_access_key", NullValueHandling = NullValueHandling.Ignore)]
[JsonPropertyName("storage_access_key")]
public string Storage_Access_Key { get; set; }

[JsonProperty("sas_token", NullValueHandling = NullValueHandling.Ignore)]
[JsonPropertyName("sas_token")]
public string Sas_Token { get; set; }

[JsonProperty("container")]
[JsonPropertyName("container")]
public string Container { get; set; }

[JsonProperty("blob", NullValueHandling = NullValueHandling.Ignore)]
[JsonPropertyName("blob")]
public string Blob { get; set; }

[JsonProperty("blob_prefix", NullValueHandling = NullValueHandling.Ignore)]
[JsonPropertyName("blob_prefix")]
public string Blob_Prefix { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace CloudConvert.API.Models.ExportOperations
{
public class ExportGoogleCloudStorageCreateRequest
{
[JsonProperty("operation")]
public static string Operation = "export/google-cloud-storage";
[JsonPropertyName("operation")]
public string Operation { get; } = "export/google-cloud-storage";

/// <summary>
/// The input task name(s) for this task.
/// input: string | string[];
/// </summary>
[JsonProperty("input")]
public dynamic Input { get; set; }
[JsonPropertyName("input")]
public object Input { get; set; }

[JsonProperty("project_id")]
[JsonPropertyName("project_id")]
public string ProjectId { get; set; }

[JsonProperty("bucket")]
[JsonPropertyName("bucket")]
public string Bucket { get; set; }

[JsonProperty("client_email")]
[JsonPropertyName("client_email")]
public string Client_Email { get; set; }

[JsonProperty("private_key")]
[JsonPropertyName("private_key")]
public string Private_Key { get; set; }

[JsonProperty("file", NullValueHandling = NullValueHandling.Ignore)]
[JsonPropertyName("file")]
public string File { get; set; }

[JsonProperty("file_prefix", NullValueHandling = NullValueHandling.Ignore)]
[JsonPropertyName("file_prefix")]
public string File_Prefix { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace CloudConvert.API.Models.ExportOperations
{
public class ExportOpenStackCreateRequest
{
[JsonProperty("operation")]
public static string Operation = "export/openstack";
[JsonPropertyName("operation")]
public string Operation { get; } = "export/openstack";

/// <summary>
/// The input task name(s) for this task.
/// input: string | string[];
/// </summary>
[JsonProperty("input")]
public dynamic Input { get; set; }
[JsonPropertyName("input")]
public object Input { get; set; }

[JsonProperty("auth_url")]
[JsonPropertyName("auth_url")]
public string Auth_Url { get; set; }

[JsonProperty("username")]
[JsonPropertyName("username")]
public string Username { get; set; }

[JsonProperty("password")]
[JsonPropertyName("password")]
public string Password { get; set; }

[JsonProperty("region")]
[JsonPropertyName("region")]
public string Region { get; set; }

[JsonProperty("container")]
[JsonPropertyName("container")]
public string Container { get; set; }

[JsonProperty("file", NullValueHandling = NullValueHandling.Ignore)]
[JsonPropertyName("file")]
public string File { get; set; }

[JsonProperty("file_prefix", NullValueHandling = NullValueHandling.Ignore)]
[JsonPropertyName("file_prefix")]
public string FilePrefix { get; set; }
}
}
Loading