1
1
using Newtonsoft . Json ;
2
+ using Supabase . Functions . Extensions ;
2
3
using Supabase . Functions . Interfaces ;
3
4
using Supabase . Functions . Responses ;
4
5
using System ;
5
6
using System . Collections . Generic ;
6
7
using System . Net . Http ;
8
+ using System . Runtime . CompilerServices ;
7
9
using System . Text ;
8
10
using System . Threading . Tasks ;
9
11
using System . Web ;
10
12
13
+ [ assembly: InternalsVisibleTo ( "FunctionsTests" ) ]
11
14
namespace Supabase . Functions
12
15
{
16
+
13
17
public class Client : IFunctionsClient
14
18
{
15
19
private static readonly HttpClient client = new HttpClient ( ) ;
20
+ private string baseUrl ;
21
+
22
+ public Func < Dictionary < string , string > > ? GetHeaders { get ; set ; }
23
+
24
+ public Client ( string baseUrl )
25
+ {
26
+ this . baseUrl = baseUrl ;
27
+ }
16
28
17
29
/// <summary>
18
30
/// Returns an <see cref="HttpContent"/> response, allowing for coersion into Streams, Strings, and byte[]
19
31
/// </summary>
20
- /// <param name="url">Url of function to invoke </param>
32
+ /// <param name="functionName">Function Name, will be appended to BaseUrl </param>
21
33
/// <param name="token">Anon Key.</param>
22
34
/// <param name="options">Options</param>
23
35
/// <returns></returns>
24
- public async Task < HttpContent > RawInvoke ( string url , string ? token = null , InvokeFunctionOptions ? options = null ) => ( await HandleRequest ( url , token , options ) ) . Content ;
36
+ public async Task < HttpContent > RawInvoke ( string functionName , string ? token = null , InvokeFunctionOptions ? options = null )
37
+ {
38
+ var url = $ "{ baseUrl } /{ functionName } ";
39
+
40
+ return ( await HandleRequest ( url , token , options ) ) . Content ;
41
+ }
25
42
26
43
/// <summary>
27
44
/// Invokes a function and returns the Text content of the response.
28
45
/// </summary>
29
- /// <param name="url">Url of the function to invoke </param>
46
+ /// <param name="functionName">Function Name, will be appended to BaseUrl </param>
30
47
/// <param name="token">Anon Key.</param>
31
48
/// <param name="options">Options</param>
32
49
/// <returns></returns>
33
- public async Task < string > Invoke ( string url , string ? token = null , InvokeFunctionOptions ? options = null )
50
+ public async Task < string > Invoke ( string functionName , string ? token = null , InvokeFunctionOptions ? options = null )
34
51
{
52
+ var url = $ "{ baseUrl } /{ functionName } ";
35
53
var response = await HandleRequest ( url , token , options ) ;
36
54
37
55
return await response . Content . ReadAsStringAsync ( ) ;
@@ -41,12 +59,13 @@ public async Task<string> Invoke(string url, string? token = null, InvokeFunctio
41
59
/// Invokes a function and returns a JSON Deserialized object according to the supplied generic Type <typeparamref name="T"/>
42
60
/// </summary>
43
61
/// <typeparam name="T"></typeparam>
44
- /// <param name="url">Url of function to invoke </param>
62
+ /// <param name="functionsName">Function Name, will be appended to BaseUrl </param>
45
63
/// <param name="token">Anon Key.</param>
46
64
/// <param name="options">Options</param>
47
65
/// <returns></returns>
48
- public async Task < T ? > Invoke < T > ( string url , string ? token = null , InvokeFunctionOptions ? options = null ) where T : class
66
+ public async Task < T ? > Invoke < T > ( string functionName , string ? token = null , InvokeFunctionOptions ? options = null ) where T : class
49
67
{
68
+ var url = $ "{ baseUrl } /{ functionName } ";
50
69
var response = await HandleRequest ( url , token , options ) ;
51
70
52
71
var content = await response . Content . ReadAsStringAsync ( ) ;
@@ -62,13 +81,18 @@ public async Task<string> Invoke(string url, string? token = null, InvokeFunctio
62
81
/// <param name="options"></param>
63
82
/// <returns></returns>
64
83
/// <exception cref="RequestException"></exception>
65
- private async Task < HttpResponseMessage > HandleRequest ( string url , string ? token = null , InvokeFunctionOptions ? options = null )
84
+ internal async Task < HttpResponseMessage > HandleRequest ( string url , string ? token = null , InvokeFunctionOptions ? options = null )
66
85
{
67
86
if ( options == null )
68
87
{
69
88
options = new InvokeFunctionOptions ( ) ;
70
89
}
71
90
91
+ if ( GetHeaders != null )
92
+ {
93
+ options . Headers = GetHeaders ( ) . MergeLeft ( options . Headers ) ;
94
+ }
95
+
72
96
if ( ! string . IsNullOrEmpty ( token ) )
73
97
{
74
98
options . Headers [ "Authorization" ] = $ "Bearer { token } ";
0 commit comments