Skip to content

Commit

Permalink
fix: multipart file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
christyjacob4 committed Oct 1, 2024
1 parent 1c75191 commit ebfcd38
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Appwrite/Appwrite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<PackageId>Appwrite</PackageId>
<Version>0.10.0</Version>
<Version>0.10.1</Version>
<Authors>Appwrite Team</Authors>
<Company>Appwrite Team</Company>
<Description>
Expand Down
24 changes: 15 additions & 9 deletions Appwrite/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Client(
{
_endpoint = endpoint;
_http = http ?? new HttpClient();

_httpForRedirect = httpForRedirect ?? new HttpClient(
new HttpClientHandler(){
AllowAutoRedirect = false
Expand All @@ -69,12 +69,11 @@ public Client(
_headers = new Dictionary<string, string>()
{
{ "content-type", "application/json" },
{ "user-agent" , "AppwriteDotNetSDK/0.10.0 (${Environment.OSVersion.Platform}; ${Environment.OSVersion.VersionString})"},
{ "user-agent" , "AppwriteDotNetSDK/0.10.1 (${Environment.OSVersion.Platform}; ${Environment.OSVersion.VersionString})"},
{ "x-sdk-name", ".NET" },
{ "x-sdk-platform", "server" },
{ "x-sdk-language", "dotnet" },
{ "x-sdk-version", "0.10.0"},
{ "X-Appwrite-Response-Format", "1.6.0" }
{ "x-sdk-version", "0.10.1"}, { "X-Appwrite-Response-Format", "1.6.0" }
};

_config = new Dictionary<string, string>();
Expand Down Expand Up @@ -252,7 +251,7 @@ private HttpRequestMessage PrepareRequest(

public async Task<String> Redirect(
string method,
string path,
string path,
Dictionary<string, string> headers,
Dictionary<string, object?> parameters)
{
Expand Down Expand Up @@ -301,7 +300,7 @@ public async Task<T> Call<T>(
var response = await _http.SendAsync(request);
var code = (int)response.StatusCode;

if (response.Headers.TryGetValues("x-appwrite-warning", out var warnings))
if (response.Headers.TryGetValues("x-appwrite-warning", out var warnings))
{
foreach (var warning in warnings)
{
Expand Down Expand Up @@ -408,16 +407,23 @@ public async Task<T> ChunkedUpload<T>(

if (!string.IsNullOrEmpty(idParamName) && (string)parameters[idParamName] != "unique()")
{
try
{
// Make a request to check if a file already exists
var current = await Call<Dictionary<string, object?>>(
method: "GET",
path: "$path/${params[idParamName]}",
headers,
parameters = new Dictionary<string, object?>()
path: $"{path}/{parameters[idParamName]}",
new Dictionary<string, string> { { "content-type", "application/json" } },
parameters: new Dictionary<string, object?>()
);
var chunksUploaded = (long)current["chunksUploaded"];
offset = chunksUploaded * ChunkSize;
}
catch (Exception ex)
{
// ignored as it mostly means file not found
}
}

while (offset < size)
{
Expand Down
3 changes: 3 additions & 0 deletions Appwrite/Enums/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public Runtime(string value)
public static Runtime Python311 => new Runtime("python-3.11");
public static Runtime Python312 => new Runtime("python-3.12");
public static Runtime PythonMl311 => new Runtime("python-ml-3.11");
public static Runtime Deno121 => new Runtime("deno-1.21");
public static Runtime Deno124 => new Runtime("deno-1.24");
public static Runtime Deno135 => new Runtime("deno-1.35");
public static Runtime Deno140 => new Runtime("deno-1.40");
public static Runtime Dart215 => new Runtime("dart-2.15");
public static Runtime Dart216 => new Runtime("dart-2.16");
Expand Down
20 changes: 10 additions & 10 deletions Appwrite/Services/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static Models.User Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// List Identities
/// List identities
/// <para>
/// Get the list of identities for the currently logged in user.
/// </para>
Expand Down Expand Up @@ -296,7 +296,7 @@ static Models.User Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Create Authenticator
/// Create authenticator
/// <para>
/// Add an authenticator app to be used as an MFA factor. Verify the
/// authenticator using the [verify
Expand Down Expand Up @@ -332,7 +332,7 @@ static Models.MfaType Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Verify Authenticator
/// Verify authenticator
/// <para>
/// Verify an authenticator app after adding it using the [add
/// authenticator](/docs/references/cloud/client-web/account#createMfaAuthenticator)
Expand Down Expand Up @@ -368,7 +368,7 @@ static Models.User Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Delete Authenticator
/// Delete authenticator
/// <para>
/// Delete an authenticator for a user by ID.
/// </para>
Expand Down Expand Up @@ -398,7 +398,7 @@ public Task<object> DeleteMfaAuthenticator(Appwrite.Enums.AuthenticatorType type
}

/// <summary>
/// Create MFA Challenge
/// Create MFA challenge
/// <para>
/// Begin the process of MFA verification after sign-in. Finish the flow with
/// [updateMfaChallenge](/docs/references/cloud/client-web/account#updateMfaChallenge)
Expand Down Expand Up @@ -433,7 +433,7 @@ static Models.MfaChallenge Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Create MFA Challenge (confirmation)
/// Create MFA challenge (confirmation)
/// <para>
/// Complete the MFA challenge by providing the one-time password. Finish the
/// process of MFA verification by providing the one-time password. To begin
Expand Down Expand Up @@ -468,7 +468,7 @@ public Task<object> UpdateMfaChallenge(string challengeId, string otp)
}

/// <summary>
/// List Factors
/// List factors
/// <para>
/// List the factors available on the account to be used as a MFA challange.
/// </para>
Expand Down Expand Up @@ -500,7 +500,7 @@ static Models.MfaFactors Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Get MFA Recovery Codes
/// Get MFA recovery codes
/// <para>
/// Get recovery codes that can be used as backup for MFA flow. Before getting
/// codes, they must be generated using
Expand Down Expand Up @@ -535,7 +535,7 @@ static Models.MfaRecoveryCodes Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Create MFA Recovery Codes
/// Create MFA recovery codes
/// <para>
/// Generate recovery codes as backup for MFA flow. It's recommended to
/// generate and show then immediately after user successfully adds their
Expand Down Expand Up @@ -571,7 +571,7 @@ static Models.MfaRecoveryCodes Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Regenerate MFA Recovery Codes
/// Regenerate MFA recovery codes
/// <para>
/// Regenerate recovery codes that can be used as backup for MFA flow. Before
/// regenerating codes, they must be first generated using
Expand Down
20 changes: 12 additions & 8 deletions Appwrite/Services/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ static Models.ExecutionList Convert(Dictionary<string, object> it) =>
/// function execution process will start asynchronously.
/// </para>
/// </summary>
public Task<Models.Execution> CreateExecution(string functionId, string? body = null, bool? xasync = null, string? xpath = null, Appwrite.Enums.ExecutionMethod? method = null, object? headers = null, string? scheduledAt = null)
public Task<Models.Execution> CreateExecution(string functionId, payload? body = null, bool? xasync = null, string? xpath = null, Appwrite.Enums.ExecutionMethod? method = null, object? headers = null, string? scheduledAt = null, Action<UploadProgress>? onProgress = null)
{
var apiPath = "/functions/{functionId}/executions"
.Replace("{functionId}", functionId);
Expand All @@ -626,20 +626,24 @@ static Models.ExecutionList Convert(Dictionary<string, object> it) =>

var apiHeaders = new Dictionary<string, string>()
{
{ "content-type", "application/json" }
{ "content-type", "multipart/form-data" }
};


static Models.Execution Convert(Dictionary<string, object> it) =>
Models.Execution.From(map: it);

return _client.Call<Models.Execution>(
method: "POST",
path: apiPath,
headers: apiHeaders,
parameters: apiParameters.Where(it => it.Value != null).ToDictionary(it => it.Key, it => it.Value)!,
convert: Convert);
string? idParamName = null;


return _client.ChunkedUpload(
apiPath,
apiHeaders,
apiParameters,
Convert,
paramName,
idParamName,
onProgress);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Appwrite/Services/Locale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static Models.Locale Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// List Locale Codes
/// List locale codes
/// <para>
/// List of all locale codes in [ISO
/// 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes).
Expand Down
2 changes: 1 addition & 1 deletion Appwrite/Services/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ static Models.File Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Delete File
/// Delete file
/// <para>
/// Delete a file by its unique ID. Only users with write permissions have
/// access to delete this resource.
Expand Down
20 changes: 10 additions & 10 deletions Appwrite/Services/Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static Models.User Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// List Identities
/// List identities
/// <para>
/// Get identities for all users.
/// </para>
Expand Down Expand Up @@ -713,7 +713,7 @@ static Models.User Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Delete Authenticator
/// Delete authenticator
/// <para>
/// Delete an authenticator app.
/// </para>
Expand Down Expand Up @@ -747,7 +747,7 @@ static Models.User Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// List Factors
/// List factors
/// <para>
/// List the factors available on the account to be used as a MFA challange.
/// </para>
Expand Down Expand Up @@ -780,7 +780,7 @@ static Models.MfaFactors Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Get MFA Recovery Codes
/// Get MFA recovery codes
/// <para>
/// Get recovery codes that can be used as backup for MFA flow by User ID.
/// Before getting codes, they must be generated using
Expand Down Expand Up @@ -816,7 +816,7 @@ static Models.MfaRecoveryCodes Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Regenerate MFA Recovery Codes
/// Regenerate MFA recovery codes
/// <para>
/// Regenerate recovery codes that can be used as backup for MFA flow by User
/// ID. Before regenerating codes, they must be first generated using
Expand Down Expand Up @@ -852,7 +852,7 @@ static Models.MfaRecoveryCodes Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Create MFA Recovery Codes
/// Create MFA recovery codes
/// <para>
/// Generate recovery codes used as backup for MFA flow for User ID. Recovery
/// codes can be used as a MFA verification type in
Expand Down Expand Up @@ -1226,7 +1226,7 @@ static Models.User Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// List User Targets
/// List user targets
/// <para>
/// List the messaging targets that are associated with a user.
/// </para>
Expand Down Expand Up @@ -1260,7 +1260,7 @@ static Models.TargetList Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Create User Target
/// Create user target
/// <para>
/// Create a messaging target.
/// </para>
Expand Down Expand Up @@ -1298,7 +1298,7 @@ static Models.Target Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Get User Target
/// Get user target
/// <para>
/// Get a user's push notification target by ID.
/// </para>
Expand Down Expand Up @@ -1332,7 +1332,7 @@ static Models.Target Convert(Dictionary<string, object> it) =>
}

/// <summary>
/// Update User target
/// Update user target
/// <para>
/// Update a messaging target.
/// </para>
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ Appwrite is an open-source backend as a service server that abstract and simplif
Add this reference to your project's `.csproj` file:

```xml
<PackageReference Include="Appwrite" Version="0.10.0" />
<PackageReference Include="Appwrite" Version="0.10.1" />
```

You can install packages from the command line:

```powershell
# Package Manager
Install-Package Appwrite -Version 0.10.0
Install-Package Appwrite -Version 0.10.1
# or .NET CLI
dotnet add package Appwrite --version 0.10.0
dotnet add package Appwrite --version 0.10.1
```


Expand Down
2 changes: 1 addition & 1 deletion docs/examples/functions/create-execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Functions functions = new Functions(client);

Execution result = await functions.CreateExecution(
functionId: "<FUNCTION_ID>",
body: "<BODY>", // optional
body: , // optional
async: false, // optional
path: "<PATH>", // optional
method: ExecutionMethod.GET, // optional
Expand Down

0 comments on commit ebfcd38

Please sign in to comment.