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 ;
72using System . Collections . Generic ;
83using System . Net . Http ;
94using System . Runtime . CompilerServices ;
105using System . Text ;
116using System . Threading . Tasks ;
127using System . Web ;
8+ using Newtonsoft . Json ;
9+ using Supabase . Core ;
10+ using Supabase . Core . Extensions ;
1311using Supabase . Functions . Exceptions ;
12+ using Supabase . Functions . Interfaces ;
1413
1514[ assembly: InternalsVisibleTo ( "FunctionsTests" ) ]
1615
@@ -21,10 +20,11 @@ public partial class Client : IFunctionsClient
2120 {
2221 private HttpClient _httpClient = new HttpClient ( ) ;
2322 private readonly string _baseUrl ;
23+ private readonly FunctionRegion _region ;
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 ; }
@@ -33,9 +33,11 @@ public partial class Client : IFunctionsClient
3333 /// Initializes a functions client
3434 /// </summary>
3535 /// <param name="baseUrl"></param>
36- public Client ( string baseUrl )
36+ /// <param name="region"></param>
37+ public Client ( string baseUrl , FunctionRegion ? region = null )
3738 {
3839 _baseUrl = baseUrl ;
40+ _region = region ?? FunctionRegion . Any ;
3941 }
4042
4143 /// <summary>
@@ -45,8 +47,11 @@ public Client(string baseUrl)
4547 /// <param name="token">Anon Key.</param>
4648 /// <param name="options">Options</param>
4749 /// <returns></returns>
48- public async Task < HttpContent > RawInvoke ( string functionName , string ? token = null ,
49- InvokeFunctionOptions ? options = null )
50+ public async Task < HttpContent > RawInvoke (
51+ string functionName ,
52+ string ? token = null ,
53+ InvokeFunctionOptions ? options = null
54+ )
5055 {
5156 var url = $ "{ _baseUrl } /{ functionName } ";
5257
@@ -60,8 +65,11 @@ public async Task<HttpContent> RawInvoke(string functionName, string? token = nu
6065 /// <param name="token">Anon Key.</param>
6166 /// <param name="options">Options</param>
6267 /// <returns></returns>
63- public async Task < string > Invoke ( string functionName , string ? token = null ,
64- InvokeFunctionOptions ? options = null )
68+ public async Task < string > Invoke (
69+ string functionName ,
70+ string ? token = null ,
71+ InvokeFunctionOptions ? options = null
72+ )
6573 {
6674 var url = $ "{ _baseUrl } /{ functionName } ";
6775 var response = await HandleRequest ( url , token , options ) ;
@@ -77,8 +85,12 @@ public async Task<string> Invoke(string functionName, string? token = null,
7785 /// <param name="token">Anon Key.</param>
7886 /// <param name="options">Options</param>
7987 /// <returns></returns>
80- public async Task < T ? > Invoke < T > ( string functionName , string ? token = null ,
81- InvokeFunctionOptions ? options = null ) where T : class
88+ public async Task < T ? > Invoke < T > (
89+ string functionName ,
90+ string ? token = null ,
91+ InvokeFunctionOptions ? options = null
92+ )
93+ where T : class
8294 {
8395 var url = $ "{ _baseUrl } /{ functionName } ";
8496 var response = await HandleRequest ( url , token , options ) ;
@@ -96,8 +108,11 @@ public async Task<string> Invoke(string functionName, string? token = null,
96108 /// <param name="options"></param>
97109 /// <returns></returns>
98110 /// <exception cref="FunctionsException"></exception>
99- private async Task < HttpResponseMessage > HandleRequest ( string url , string ? token = null ,
100- InvokeFunctionOptions ? options = null )
111+ private async Task < HttpResponseMessage > HandleRequest (
112+ string url ,
113+ string ? token = null ,
114+ InvokeFunctionOptions ? options = null
115+ )
101116 {
102117 options ??= new InvokeFunctionOptions ( ) ;
103118
@@ -113,26 +128,40 @@ private async Task<HttpResponseMessage> HandleRequest(string url, string? token
113128
114129 options . Headers [ "X-Client-Info" ] = Util . GetAssemblyVersion ( typeof ( Client ) ) ;
115130
131+ var region = options . FunctionRegion ;
132+ if ( region == null )
133+ {
134+ region = _region ;
135+ }
136+
137+ if ( region != FunctionRegion . Any )
138+ {
139+ options . Headers [ "x-region" ] = region . ToString ( ) ;
140+ }
141+
116142 var builder = new UriBuilder ( url ) ;
117143 var query = HttpUtility . ParseQueryString ( builder . Query ) ;
118144
119145 builder . Query = query . ToString ( ) ;
120146
121- using var requestMessage = new HttpRequestMessage ( HttpMethod . Post , builder . Uri ) ;
122- requestMessage . Content = new StringContent ( JsonConvert . SerializeObject ( options . Body ) , Encoding . UTF8 ,
123- "application/json" ) ;
147+ using var requestMessage = new HttpRequestMessage ( options . HttpMethod , builder . Uri ) ;
148+ requestMessage . Content = new StringContent (
149+ JsonConvert . SerializeObject ( options . Body ) ,
150+ Encoding . UTF8 ,
151+ "application/json"
152+ ) ;
124153
125154 foreach ( var kvp in options . Headers )
126155 {
127156 requestMessage . Headers . TryAddWithoutValidation ( kvp . Key , kvp . Value ) ;
128157 }
129-
158+
130159 if ( _httpClient . Timeout != options . HttpTimeout )
131160 {
132161 _httpClient = new HttpClient ( ) ;
133162 _httpClient . Timeout = options . HttpTimeout ;
134163 }
135-
164+
136165 var response = await _httpClient . SendAsync ( requestMessage ) ;
137166
138167 if ( response . IsSuccessStatusCode && ! response . Headers . Contains ( "x-relay-error" ) )
@@ -143,10 +172,10 @@ private async Task<HttpResponseMessage> HandleRequest(string url, string? token
143172 {
144173 Content = content ,
145174 Response = response ,
146- StatusCode = ( int ) response . StatusCode
175+ StatusCode = ( int ) response . StatusCode ,
147176 } ;
148177 exception . AddReason ( ) ;
149178 throw exception ;
150179 }
151180 }
152- }
181+ }
0 commit comments