Skip to content

Commit

Permalink
Added handles for envelope download
Browse files Browse the repository at this point in the history
liblab SDK update v1.1.0
  • Loading branch information
upcFrost authored Jan 15, 2025
2 parents 7183c4e + 220271f commit 9fc060a
Show file tree
Hide file tree
Showing 155 changed files with 2,528 additions and 409 deletions.
265 changes: 172 additions & 93 deletions .manifest.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Signplus/.gitignore → Alohi.Signplus/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,7 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml

# OS generated files
.DS_Store
Thumbs.db
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Signplus</RootNamespace>
<Version>1.0.0</Version>
<RootNamespace>Alohi.Signplus</RootNamespace>
<Version>1.1.0</Version>
<PackageId>Alohi.Signplus</PackageId>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\" />
<PackageReference Include="Polly" Version="8.2.1" />
<None Include="../README.md" Pack="true" PackagePath="\" />
<PackageReference Include="FluentValidation" Version="11.5.0" />
<PackageReference Include="Polly" Version="8.2.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Environment = Signplus.Http.Environment;
using Environment = Alohi.Signplus.Http.Environment;

namespace Signplus.Config;
namespace Alohi.Signplus.Config;

/// <summary>
/// Configuration options for the SignplusClient.
Expand Down
16 changes: 16 additions & 0 deletions Alohi.Signplus/Http/ApiException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Alohi.Signplus.Http.Exceptions;

public class ApiException : HttpRequestException
{
public HttpResponseMessage Response { get; }

public ApiException(HttpResponseMessage responseMessage)
: base(
$"Response status code does not indicate success: {(int)responseMessage.StatusCode} ({responseMessage.StatusCode}).",
null,
responseMessage.StatusCode
)
{
Response = responseMessage;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
namespace Signplus.Http;
namespace Alohi.Signplus.Http;

using Alohi.Signplus.Http.Extensions;

/// <summary>
/// The environments available for the SDK.
/// </summary>
public class Environment
{
internal Uri Uri { get; private set; }
internal string OriginalString { get; private set; }

private Environment(string uri)
{
OriginalString = uri;
Uri = new Uri(uri);
}

Expand Down
15 changes: 15 additions & 0 deletions Alohi.Signplus/Http/Extensions/HttpResponseMessageExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Alohi.Signplus.Http.Exceptions;

namespace Alohi.Signplus.Http.Extensions;

public static class HttpResponseMessageExtensions
{
public static HttpResponseMessage EnsureSuccessfulResponse(this HttpResponseMessage response)
{
if (!response.IsSuccessStatusCode)
{
throw new ApiException(response);
}
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Net.Http.Headers;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Alohi.Signplus.Http.Extensions;

public static class MultipartFormDataContentExtensions
{
public static MultipartFormDataContent AddObject(
this MultipartFormDataContent formData,
object content,
JsonSerializerOptions? options
)
{
foreach (var property in content.GetType().GetProperties())
{
var value = property.GetValue(content);
var key = property.Name;
var mappedKey = GetPropertyName(property);

if (value is byte[] fileBytes)
{
var fileContent = new ByteArrayContent(fileBytes);
fileContent.Headers.ContentType = new MediaTypeHeaderValue(
"application/octet-stream"
);

formData.Add(fileContent, mappedKey, mappedKey);
}
else if (value != null && !IsPrimitive(property.PropertyType))
{
var nestedContent = new MultipartFormDataContent().AddObject(value, options);
formData.Add(nestedContent, mappedKey);
}
else
{
formData.Add(new StringContent(value?.ToString() ?? string.Empty), mappedKey);
}
}

return formData;
}

private static bool IsPrimitive(Type type)
{
return type.IsPrimitive || type.IsValueType || type == typeof(string);
}

private static string GetPropertyName(PropertyInfo property)
{
var jsonPropertyAttribute = property.GetCustomAttribute<JsonPropertyNameAttribute>();
if (jsonPropertyAttribute != null)
{
return jsonPropertyAttribute.Name;
}
return property.Name;
}
}
18 changes: 18 additions & 0 deletions Alohi.Signplus/Http/Extensions/UriExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Alohi.Signplus.Http.Extensions;

public static class UriExtensions
{
public static Uri EnsureTrailingSlash(this Uri uri)
{
if (uri == null)
throw new ArgumentNullException(nameof(uri));

var uriString = uri.ToString();
if (!uriString.EndsWith("/"))
{
uriString += "/";
}

return new Uri(uriString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Polly;
using Polly.Retry;

namespace Signplus.Http.Handlers;
namespace Alohi.Signplus.Http.Handlers;

/// <summary>
/// A handler for retrying requests when they fail.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Signplus.Http.Handlers;
namespace Alohi.Signplus.Http.Handlers;

/// <summary>
/// A handler for adding a token to the request.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using Signplus.Http.Serialization;
using Alohi.Signplus.Http.Extensions;
using Alohi.Signplus.Http.Serialization;

namespace Signplus.Http;
namespace Alohi.Signplus.Http;

/// <summary>
/// A builder for creating <see cref="HttpRequestMessage"/> instances with full support for serialization.
Expand Down Expand Up @@ -108,9 +111,72 @@ public RequestBuilder SetOptionalHeader(string key, object? value, bool explode
/// <summary>
/// Sets the content of the request as JSON.
/// </summary>
public RequestBuilder SetContentAsJson(object content, JsonSerializerOptions? options = null)
public RequestBuilder SetContentAsJson(
object content,
JsonSerializerOptions? options = null,
MediaTypeHeaderValue? mediaType = null
)
{
_content = JsonContent.Create(content, mediaType, options);
return this;
}

/// <summary>
/// Sets the content of the request as Text.
/// </summary>
public RequestBuilder SetContentAsText(
string content,
string mediaType,
Encoding? encoding = null
)
{
encoding ??= Encoding.UTF8;
_content = new StringContent(content, encoding, mediaType);
return this;
}

/// <summary>
/// Sets the content of the request as Binary.
/// </summary>
public RequestBuilder SetContentAsBinary(byte[] content, MediaTypeHeaderValue mediaType)
{
_content = new ByteArrayContent(content);
_content.Headers.ContentType = mediaType;
return this;
}

/// <summary>
/// Sets the content of the request as application/x-www-form-urlencoded.
/// </summary>
public RequestBuilder SetUrlEncodedContent(
object content,
JsonSerializerOptions? options = null
)
{
var jsonContent = JsonSerializer.Serialize(content, options);
var dictionary = JsonSerializer.Deserialize<Dictionary<string, string>>(
jsonContent,
options
);

if (dictionary is null)
{
throw new ArgumentException("Invalid content for form-urlencoded content type.");
}

_content = new FormUrlEncodedContent(dictionary);
return this;
}

/// <summary>
/// Sets the content of the request as multipart/form-data.
/// </summary>
public RequestBuilder SetContentAsMultipartFormData(
object content,
JsonSerializerOptions? options = null
)
{
_content = JsonContent.Create(content, options: options);
_content = new MultipartFormDataContent().AddObject(content, options);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Signplus.Http.Serialization;
namespace Alohi.Signplus.Http.Serialization;

public enum PathSerializationStyle
{
Simple = SerializationStyle.Simple,
Label = SerializationStyle.Label,
Matrix = SerializationStyle.Matrix
Matrix = SerializationStyle.Matrix,
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Signplus.Http.Serialization;
namespace Alohi.Signplus.Http.Serialization;

public enum QuerySerializationStyle
{
Form = SerializationStyle.Form,
SpaceDelimited = SerializationStyle.SpaceDelimited,
PipeDelimited = SerializationStyle.PipeDelimited,
DeepObject = SerializationStyle.DeepObject
DeepObject = SerializationStyle.DeepObject,
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Signplus.Http.Serialization;
namespace Alohi.Signplus.Http.Serialization;

public enum SerializationStyle
{
Expand All @@ -8,5 +8,5 @@ public enum SerializationStyle
Form = 3,
SpaceDelimited = 4,
PipeDelimited = 5,
DeepObject = 6
DeepObject = 6,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Net;
using System.Text.Json.Serialization;

namespace Signplus.Http.Serialization;
namespace Alohi.Signplus.Http.Serialization;

public static class Serializer
{
Expand All @@ -20,13 +20,11 @@ public static string Serialize<T>(
{
return value switch
{
null
or string
or bool
or int
or long
or double
=> SerializePrimitive(key, value, style),
null or string or bool or int or long or double => SerializePrimitive(
key,
value,
style
),
IEnumerable e => SerializeEnumerable(key, e, style, explode),
object o => SerializeObject(key, o, style, explode),
};
Expand All @@ -40,9 +38,13 @@ private static string SerializeValue(object? value)
string s => WebUtility.UrlEncode(s),
bool b => b.ToString().ToLowerInvariant(),
int or long or double => value.ToString(),
IEnumerable e
=> SerializeEnumerable(string.Empty, e, SerializationStyle.Simple, false),
not null => SerializeObject(string.Empty, value, SerializationStyle.Simple, false)
IEnumerable e => SerializeEnumerable(
string.Empty,
e,
SerializationStyle.Simple,
false
),
not null => SerializeObject(string.Empty, value, SerializationStyle.Simple, false),
} ?? string.Empty;
}

Expand All @@ -53,7 +55,7 @@ private static string SerializePrimitive(string key, object? value, Serializatio
SerializationStyle.Label => $".{SerializeValue(value)}",
SerializationStyle.Matrix => $";{SerializeValue(value)}",
SerializationStyle.Form => $"{key}={SerializeValue(value)}",
_ => SerializeValue(value)
_ => SerializeValue(value),
};
}

Expand Down Expand Up @@ -91,7 +93,7 @@ bool explode
{
SerializationStyle.SpaceDelimited => " ",
SerializationStyle.PipeDelimited => "|",
_ => ","
_ => ",",
};

return $"{key}={string.Join(separator, serializedValues)}";
Expand Down
16 changes: 16 additions & 0 deletions Alohi.Signplus/Http/ValidationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Alohi.Signplus.Http.Exceptions;

using System;
using FluentValidation.Results;

public class ValidationException : Exception
{
public List<ValidationFailure> ValidationFailure { get; }

public ValidationException(List<ValidationFailure> validationFailure)
: base("Validation failed.")
{
ValidationFailure =
validationFailure ?? throw new ArgumentNullException(nameof(validationFailure));
}
}
Loading

0 comments on commit 9fc060a

Please sign in to comment.