Skip to content

Commit 4d5df7d

Browse files
committed
Revert namespacing for C#8 support
1 parent 1cfafe6 commit 4d5df7d

File tree

9 files changed

+329
-315
lines changed

9 files changed

+329
-315
lines changed
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
using System;
22
namespace Supabase.Postgrest.Extensions
33
{
4-
/// <summary>
5-
/// Pull the instance info out of the Uri
6-
/// </summary>
7-
public static class UriExtensions
8-
{
9-
/// <summary>
10-
/// Pull the instance info out of the Uri
11-
/// </summary>
12-
/// <param name="uri"></param>
13-
/// <returns></returns>
14-
public static string GetInstanceUrl(this Uri uri) =>
15-
uri.GetLeftPart(UriPartial.Authority) + uri.LocalPath;
16-
}
17-
}
4+
/// <summary>
5+
/// Pull the instance info out of the Uri
6+
/// </summary>
7+
public static class UriExtensions
8+
{
9+
/// <summary>
10+
/// Pull the instance info out of the Uri
11+
/// </summary>
12+
/// <param name="uri"></param>
13+
/// <returns></returns>
14+
public static string GetInstanceUrl(this Uri uri) =>
15+
uri.GetLeftPart(UriPartial.Authority) + uri.LocalPath;
16+
}
17+
}

Postgrest/Helpers.cs

Lines changed: 147 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -19,142 +19,151 @@
1919
namespace Supabase.Postgrest
2020
{
2121

22-
internal static class Helpers
23-
{
24-
private static readonly HttpClient Client = new HttpClient();
25-
26-
private static readonly Guid AppSession = Guid.NewGuid();
27-
28-
/// <summary>
29-
/// Helper to make a request using the defined parameters to an API Endpoint and coerce into a model.
30-
/// </summary>
31-
/// <typeparam name="T"></typeparam>
32-
/// <param name="clientOptions"></param>
33-
/// <param name="method"></param>
34-
/// <param name="url"></param>
35-
/// <param name="data"></param>
36-
/// <param name="headers"></param>
37-
/// <param name="serializerSettings"></param>
38-
/// <param name="getHeaders"></param>
39-
/// <param name="cancellationToken"></param>
40-
/// <returns></returns>
41-
public static async Task<ModeledResponse<T>> MakeRequest<T>(ClientOptions clientOptions, HttpMethod method, string url, JsonSerializerSettings serializerSettings, object? data = null,
42-
Dictionary<string, string>? headers = null, Func<Dictionary<string, string>>? getHeaders = null, CancellationToken cancellationToken = default) where T : BaseModel, new()
43-
{
44-
var baseResponse = await MakeRequest(clientOptions, method, url, serializerSettings, data, headers, cancellationToken);
45-
return new ModeledResponse<T>(baseResponse, serializerSettings, getHeaders);
46-
}
47-
48-
/// <summary>
49-
/// Helper to make a request using the defined parameters to an API Endpoint.
50-
/// </summary>
51-
/// <param name="clientOptions"></param>
52-
/// <param name="method"></param>
53-
/// <param name="url"></param>
54-
/// <param name="data"></param>
55-
/// <param name="headers"></param>
56-
/// <param name="serializerSettings"></param>
57-
/// <param name="cancellationToken"></param>
58-
/// <returns></returns>
59-
public static async Task<BaseResponse> MakeRequest(ClientOptions clientOptions, HttpMethod method, string url, JsonSerializerSettings serializerSettings, object? data = null,
60-
Dictionary<string, string>? headers = null, CancellationToken cancellationToken = default)
61-
{
62-
var builder = new UriBuilder(url);
63-
var query = HttpUtility.ParseQueryString(builder.Query);
64-
65-
if (data != null && method == HttpMethod.Get)
66-
{
67-
// Case if it's a Get request the data object is a dictionary<string,string>
68-
if (data is Dictionary<string, string> reqParams)
69-
{
70-
foreach (var param in reqParams)
71-
query[param.Key] = param.Value;
72-
}
73-
}
74-
75-
builder.Query = query.ToString();
76-
77-
using var requestMessage = new HttpRequestMessage(method, builder.Uri);
78-
79-
if (data != null && method != HttpMethod.Get)
80-
{
81-
var stringContent = JsonConvert.SerializeObject(data, serializerSettings);
82-
83-
if (!string.IsNullOrWhiteSpace(stringContent) && JToken.Parse(stringContent).HasValues)
84-
{
85-
requestMessage.Content = new StringContent(stringContent, Encoding.UTF8, "application/json");
86-
}
87-
}
88-
89-
if (headers != null)
90-
{
91-
foreach (var kvp in headers)
92-
{
93-
requestMessage.Headers.TryAddWithoutValidation(kvp.Key, kvp.Value);
94-
}
95-
}
96-
97-
using var response = await Client.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false);
98-
var content = await response.Content.ReadAsStringAsync();
99-
100-
if (response.IsSuccessStatusCode)
101-
return new BaseResponse(clientOptions, response, content);
102-
103-
var exception = new PostgrestException(content)
104-
{
105-
Content = content,
106-
Response = response,
107-
StatusCode = (int)response.StatusCode
108-
};
109-
exception.AddReason();
110-
throw exception;
111-
}
112-
113-
/// <summary>
114-
/// Prepares the request with appropriate HTTP headers expected by Postgrest.
115-
/// </summary>
116-
/// <param name="method"></param>
117-
/// <param name="headers"></param>
118-
/// <param name="options"></param>
119-
/// <param name="rangeFrom"></param>
120-
/// <param name="rangeTo"></param>
121-
/// <returns></returns>
122-
public static Dictionary<string, string> PrepareRequestHeaders(HttpMethod method, Dictionary<string, string>? headers = null, ClientOptions? options = null, int rangeFrom = int.MinValue, int rangeTo = int.MinValue)
123-
{
124-
options ??= new ClientOptions();
125-
126-
headers = headers == null ? new Dictionary<string, string>(options.Headers) : options.Headers.MergeLeft(headers);
127-
128-
if (!string.IsNullOrEmpty(options.Schema))
129-
{
130-
headers.Add(method == HttpMethod.Get ? "Accept-Profile" : "Content-Profile", options.Schema);
131-
}
132-
133-
if (rangeFrom != int.MinValue)
134-
{
135-
var formatRangeTo = rangeTo != int.MinValue ? rangeTo.ToString() : null;
136-
137-
headers.Add("Range-Unit", "items");
138-
headers.Add("Range", $"{rangeFrom}-{formatRangeTo}");
139-
}
140-
141-
if (!headers.ContainsKey("X-Client-Info"))
142-
{
143-
try
144-
{
145-
// Default version to match other clients
146-
// https://github.com/search?q=org%3Asupabase-community+x-client-info&type=code
147-
headers.Add("X-Client-Info", $"postgrest-csharp/{Util.GetAssemblyVersion(typeof(Client))}");
148-
}
149-
catch (Exception)
150-
{
151-
// Fallback for when the version can't be found
152-
// e.g. running in the Unity Editor, ILL2CPP builds, etc.
153-
headers.Add("X-Client-Info", $"postgrest-csharp/session-{AppSession}");
154-
}
155-
}
156-
157-
return headers;
158-
}
159-
}
22+
internal static class Helpers
23+
{
24+
private static readonly HttpClient Client = new HttpClient();
25+
26+
private static readonly Guid AppSession = Guid.NewGuid();
27+
28+
/// <summary>
29+
/// Helper to make a request using the defined parameters to an API Endpoint and coerce into a model.
30+
/// </summary>
31+
/// <typeparam name="T"></typeparam>
32+
/// <param name="clientOptions"></param>
33+
/// <param name="method"></param>
34+
/// <param name="url"></param>
35+
/// <param name="data"></param>
36+
/// <param name="headers"></param>
37+
/// <param name="serializerSettings"></param>
38+
/// <param name="getHeaders"></param>
39+
/// <param name="cancellationToken"></param>
40+
/// <returns></returns>
41+
public static async Task<ModeledResponse<T>> MakeRequest<T>(ClientOptions clientOptions, HttpMethod method, string url, JsonSerializerSettings serializerSettings, object? data = null,
42+
Dictionary<string, string>? headers = null, Func<Dictionary<string, string>>? getHeaders = null, CancellationToken cancellationToken = default) where T : BaseModel, new()
43+
{
44+
var baseResponse = await MakeRequest(clientOptions, method, url, serializerSettings, data, headers, cancellationToken);
45+
return new ModeledResponse<T>(baseResponse, serializerSettings, getHeaders);
46+
}
47+
48+
/// <summary>
49+
/// Helper to make a request using the defined parameters to an API Endpoint.
50+
/// </summary>
51+
/// <param name="clientOptions"></param>
52+
/// <param name="method"></param>
53+
/// <param name="url"></param>
54+
/// <param name="data"></param>
55+
/// <param name="headers"></param>
56+
/// <param name="serializerSettings"></param>
57+
/// <param name="cancellationToken"></param>
58+
/// <returns></returns>
59+
public static async Task<BaseResponse> MakeRequest(ClientOptions clientOptions, HttpMethod method, string url, JsonSerializerSettings serializerSettings, object? data = null,
60+
Dictionary<string, string>? headers = null, CancellationToken cancellationToken = default)
61+
{
62+
var builder = new UriBuilder(url);
63+
var query = HttpUtility.ParseQueryString(builder.Query);
64+
65+
UnityEngine.Debug.Log(query);
66+
67+
if (data != null && method == HttpMethod.Get)
68+
{
69+
// Case if it's a Get request the data object is a dictionary<string,string>
70+
if (data is Dictionary<string, string> reqParams)
71+
{
72+
foreach (var param in reqParams)
73+
query[param.Key] = param.Value;
74+
}
75+
}
76+
UnityEngine.Debug.Log(data);
77+
78+
builder.Query = query.ToString();
79+
80+
UnityEngine.Debug.Log(method);
81+
UnityEngine.Debug.Log(builder.Uri);
82+
using var requestMessage = new HttpRequestMessage(method, builder.Uri);
83+
84+
if (data != null && method != HttpMethod.Get)
85+
{
86+
var stringContent = JsonConvert.SerializeObject(data, serializerSettings);
87+
88+
if (!string.IsNullOrWhiteSpace(stringContent) && JToken.Parse(stringContent).HasValues)
89+
{
90+
requestMessage.Content = new StringContent(stringContent, Encoding.UTF8, "application/json");
91+
}
92+
}
93+
UnityEngine.Debug.Log(requestMessage);
94+
95+
if (headers != null)
96+
{
97+
foreach (var kvp in headers)
98+
{
99+
requestMessage.Headers.TryAddWithoutValidation(kvp.Key, kvp.Value);
100+
}
101+
}
102+
UnityEngine.Debug.Log(headers);
103+
104+
using var response = await Client.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false);
105+
UnityEngine.Debug.Log(response);
106+
var content = await response.Content.ReadAsStringAsync();
107+
UnityEngine.Debug.Log(content);
108+
109+
if (response.IsSuccessStatusCode)
110+
return new BaseResponse(clientOptions, response, content);
111+
112+
var exception = new PostgrestException(content)
113+
{
114+
Content = content,
115+
Response = response,
116+
StatusCode = (int)response.StatusCode
117+
};
118+
exception.AddReason();
119+
throw exception;
120+
}
121+
122+
/// <summary>
123+
/// Prepares the request with appropriate HTTP headers expected by Postgrest.
124+
/// </summary>
125+
/// <param name="method"></param>
126+
/// <param name="headers"></param>
127+
/// <param name="options"></param>
128+
/// <param name="rangeFrom"></param>
129+
/// <param name="rangeTo"></param>
130+
/// <returns></returns>
131+
public static Dictionary<string, string> PrepareRequestHeaders(HttpMethod method, Dictionary<string, string>? headers = null, ClientOptions? options = null, int rangeFrom = int.MinValue, int rangeTo = int.MinValue)
132+
{
133+
options ??= new ClientOptions();
134+
135+
headers = headers == null ? new Dictionary<string, string>(options.Headers) : options.Headers.MergeLeft(headers);
136+
137+
if (!string.IsNullOrEmpty(options.Schema))
138+
{
139+
headers.Add(method == HttpMethod.Get ? "Accept-Profile" : "Content-Profile", options.Schema);
140+
}
141+
142+
if (rangeFrom != int.MinValue)
143+
{
144+
var formatRangeTo = rangeTo != int.MinValue ? rangeTo.ToString() : null;
145+
146+
headers.Add("Range-Unit", "items");
147+
headers.Add("Range", $"{rangeFrom}-{formatRangeTo}");
148+
}
149+
150+
if (!headers.ContainsKey("X-Client-Info"))
151+
{
152+
try
153+
{
154+
// Default version to match other clients
155+
// https://github.com/search?q=org%3Asupabase-community+x-client-info&type=code
156+
headers.Add("X-Client-Info", $"postgrest-csharp/{Util.GetAssemblyVersion(typeof(Client))}");
157+
}
158+
catch (Exception)
159+
{
160+
// Fallback for when the version can't be found
161+
// e.g. running in the Unity Editor, ILL2CPP builds, etc.
162+
headers.Add("X-Client-Info", $"postgrest-csharp/session-{AppSession}");
163+
}
164+
}
165+
166+
return headers;
167+
}
168+
}
160169
}

0 commit comments

Comments
 (0)