Skip to content

Commit 92061e6

Browse files
committed
feat: insert option to choose http method
1 parent 781d4f5 commit 92061e6

File tree

3 files changed

+72
-45
lines changed

3 files changed

+72
-45
lines changed

Functions/Client.cs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
using Newtonsoft.Json;
2-
using Supabase.Core;
3-
using Supabase.Core.Extensions;
4-
using Supabase.Functions.Interfaces;
5-
using Supabase.Functions.Responses;
6-
using System;
1+
using System;
72
using System.Collections.Generic;
83
using System.Net.Http;
94
using System.Runtime.CompilerServices;
105
using System.Text;
116
using System.Threading.Tasks;
127
using System.Web;
8+
using Newtonsoft.Json;
9+
using Supabase.Core;
10+
using Supabase.Core.Extensions;
1311
using Supabase.Functions.Exceptions;
12+
using Supabase.Functions.Interfaces;
13+
using Supabase.Functions.Responses;
1414

1515
[assembly: InternalsVisibleTo("FunctionsTests")]
1616

@@ -24,7 +24,7 @@ public partial class Client : IFunctionsClient
2424

2525
/// <summary>
2626
/// Function that can be set to return dynamic headers.
27-
///
27+
///
2828
/// Headers specified in the method parameters will ALWAYS take precedence over headers returned by this function.
2929
/// </summary>
3030
public Func<Dictionary<string, string>>? GetHeaders { get; set; }
@@ -45,8 +45,11 @@ public Client(string baseUrl)
4545
/// <param name="token">Anon Key.</param>
4646
/// <param name="options">Options</param>
4747
/// <returns></returns>
48-
public async Task<HttpContent> RawInvoke(string functionName, string? token = null,
49-
InvokeFunctionOptions? options = null)
48+
public async Task<HttpContent> RawInvoke(
49+
string functionName,
50+
string? token = null,
51+
InvokeFunctionOptions? options = null
52+
)
5053
{
5154
var url = $"{_baseUrl}/{functionName}";
5255

@@ -60,8 +63,11 @@ public async Task<HttpContent> RawInvoke(string functionName, string? token = nu
6063
/// <param name="token">Anon Key.</param>
6164
/// <param name="options">Options</param>
6265
/// <returns></returns>
63-
public async Task<string> Invoke(string functionName, string? token = null,
64-
InvokeFunctionOptions? options = null)
66+
public async Task<string> Invoke(
67+
string functionName,
68+
string? token = null,
69+
InvokeFunctionOptions? options = null
70+
)
6571
{
6672
var url = $"{_baseUrl}/{functionName}";
6773
var response = await HandleRequest(url, token, options);
@@ -77,8 +83,12 @@ public async Task<string> Invoke(string functionName, string? token = null,
7783
/// <param name="token">Anon Key.</param>
7884
/// <param name="options">Options</param>
7985
/// <returns></returns>
80-
public async Task<T?> Invoke<T>(string functionName, string? token = null,
81-
InvokeFunctionOptions? options = null) where T : class
86+
public async Task<T?> Invoke<T>(
87+
string functionName,
88+
string? token = null,
89+
InvokeFunctionOptions? options = null
90+
)
91+
where T : class
8292
{
8393
var url = $"{_baseUrl}/{functionName}";
8494
var response = await HandleRequest(url, token, options);
@@ -96,8 +106,11 @@ public async Task<string> Invoke(string functionName, string? token = null,
96106
/// <param name="options"></param>
97107
/// <returns></returns>
98108
/// <exception cref="FunctionsException"></exception>
99-
private async Task<HttpResponseMessage> HandleRequest(string url, string? token = null,
100-
InvokeFunctionOptions? options = null)
109+
private async Task<HttpResponseMessage> HandleRequest(
110+
string url,
111+
string? token = null,
112+
InvokeFunctionOptions? options = null
113+
)
101114
{
102115
options ??= new InvokeFunctionOptions();
103116

@@ -118,21 +131,24 @@ private async Task<HttpResponseMessage> HandleRequest(string url, string? token
118131

119132
builder.Query = query.ToString();
120133

121-
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, builder.Uri);
122-
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(options.Body), Encoding.UTF8,
123-
"application/json");
134+
using var requestMessage = new HttpRequestMessage(options.HttpMethod, builder.Uri);
135+
requestMessage.Content = new StringContent(
136+
JsonConvert.SerializeObject(options.Body),
137+
Encoding.UTF8,
138+
"application/json"
139+
);
124140

125141
foreach (var kvp in options.Headers)
126142
{
127143
requestMessage.Headers.TryAddWithoutValidation(kvp.Key, kvp.Value);
128144
}
129-
145+
130146
if (_httpClient.Timeout != options.HttpTimeout)
131147
{
132148
_httpClient = new HttpClient();
133149
_httpClient.Timeout = options.HttpTimeout;
134150
}
135-
151+
136152
var response = await _httpClient.SendAsync(requestMessage);
137153

138154
if (response.IsSuccessStatusCode && !response.Headers.Contains("x-relay-error"))
@@ -143,10 +159,11 @@ private async Task<HttpResponseMessage> HandleRequest(string url, string? token
143159
{
144160
Content = content,
145161
Response = response,
146-
StatusCode = (int)response.StatusCode
162+
StatusCode = (int)response.StatusCode,
147163
};
148164
exception.AddReason();
149165
throw exception;
150166
}
151167
}
152-
}
168+
}
169+

Functions/InvokeFunctionOptions.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
using System;
2-
using Newtonsoft.Json;
32
using System.Collections.Generic;
3+
using System.Net.Http;
4+
using Newtonsoft.Json;
45

56
namespace Supabase.Functions
67
{
78
public partial class Client
89
{
910
/// <summary>
1011
/// Options that can be supplied to a function invocation.
11-
///
12+
///
1213
/// Note: If Headers.Authorization is set, it can be later overriden if a token is supplied in the method call.
1314
/// </summary>
1415
public class InvokeFunctionOptions
1516
{
1617
/// <summary>
1718
/// Headers to be included on the request.
1819
/// </summary>
19-
public Dictionary<string, string> Headers { get; set; } = new Dictionary<string, string>();
20-
20+
public Dictionary<string, string> Headers { get; set; } =
21+
new Dictionary<string, string>();
2122

2223
/// <summary>
2324
/// Body of the Request
@@ -30,6 +31,11 @@ public class InvokeFunctionOptions
3031
/// https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.timeout?view=net-8.0#remarks
3132
/// </summary>
3233
public TimeSpan HttpTimeout { get; set; } = TimeSpan.FromSeconds(100);
34+
35+
/// <summary>
36+
/// Http method of the Request
37+
/// </summary>
38+
public HttpMethod HttpMethod { get; set; } = HttpMethod.Post;
3339
}
3440
}
35-
}
41+
}

FunctionsTests/ClientTests.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,38 @@ public async Task Invokes()
2828
{
2929
const string function = "hello";
3030

31-
var result = await _client.Invoke(function, _token, new InvokeFunctionOptions
32-
{
33-
Body = new Dictionary<string, object>
31+
var result = await _client.Invoke(
32+
function,
33+
_token,
34+
new InvokeFunctionOptions
3435
{
35-
{"name", "supabase" }
36+
Body = new Dictionary<string, object> { { "name", "supabase" } },
3637
}
37-
});
38+
);
3839

3940
Assert.IsTrue(result.Contains("supabase"));
4041

41-
42-
var result2 = await _client.Invoke<Dictionary<string, string>>(function, _token, new InvokeFunctionOptions
43-
{
44-
Body = new Dictionary<string, object>
42+
var result2 = await _client.Invoke<Dictionary<string, string>>(
43+
function,
44+
_token,
45+
new InvokeFunctionOptions
4546
{
46-
{ "name", "functions" }
47+
Body = new Dictionary<string, object> { { "name", "functions" } },
4748
}
48-
});
49+
);
4950

5051
Assert.IsInstanceOfType(result2, typeof(Dictionary<string, string>));
5152
Assert.IsTrue(result2.ContainsKey("message"));
5253
Assert.IsTrue(result2["message"].Contains("functions"));
5354

54-
55-
var result3 = await _client.RawInvoke(function, _token, new InvokeFunctionOptions
56-
{
57-
Body = new Dictionary<string, object>
55+
var result3 = await _client.RawInvoke(
56+
function,
57+
_token,
58+
new InvokeFunctionOptions
5859
{
59-
{ "name", "functions" }
60+
Body = new Dictionary<string, object> { { "name", "functions" } },
6061
}
61-
});
62+
);
6263

6364
var bytes = await result3.ReadAsByteArrayAsync();
6465

@@ -71,7 +72,10 @@ private static string GenerateToken(string secret)
7172

7273
var tokenDescriptor = new SecurityTokenDescriptor
7374
{
74-
SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256Signature)
75+
SigningCredentials = new SigningCredentials(
76+
signingKey,
77+
SecurityAlgorithms.HmacSha256Signature
78+
),
7579
};
7680

7781
var tokenHandler = new JwtSecurityTokenHandler();

0 commit comments

Comments
 (0)