Skip to content

Commit 563f37b

Browse files
committed
- Enable nullability in project (ref: supabase-community/supabase-csharp#34)
- Restructure to support DI (implements #1)
1 parent 503049f commit 563f37b

File tree

7 files changed

+117
-96
lines changed

7 files changed

+117
-96
lines changed

Diff for: CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.1.0 - 2022-11-04
4+
5+
- `Client` is no longer a Singleton class, it should be initialized using a default constructor.
6+
- [#1](https://github.com/supabase-community/functions-csharp/issues/1) Restructures library to support DI.
7+
38
## 1.0.1 - 2022-04-15
49

510
- Default `token` to be `null` in `Invoke` calls to allow `Authorization` to be passed solely via Headers.

Diff for: Functions/Client.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Newtonsoft.Json;
2+
using Supabase.Functions.Interfaces;
23
using Supabase.Functions.Responses;
34
using System;
45
using System.Collections.Generic;
@@ -9,7 +10,7 @@
910

1011
namespace Supabase.Functions
1112
{
12-
public class Client
13+
public class Client : IFunctionsClient
1314
{
1415
private static readonly HttpClient client = new HttpClient();
1516

@@ -20,7 +21,7 @@ public class Client
2021
/// <param name="token">Anon Key.</param>
2122
/// <param name="options">Options</param>
2223
/// <returns></returns>
23-
public static async Task<HttpContent> RawInvoke(string url, string token = null, InvokeFunctionOptions options = null) => (await HandleRequest(url, token, options)).Content;
24+
public async Task<HttpContent> RawInvoke(string url, string? token = null, InvokeFunctionOptions? options = null) => (await HandleRequest(url, token, options)).Content;
2425

2526
/// <summary>
2627
/// Invokes a function and returns the Text content of the response.
@@ -29,7 +30,7 @@ public class Client
2930
/// <param name="token">Anon Key.</param>
3031
/// <param name="options">Options</param>
3132
/// <returns></returns>
32-
public static async Task<string> Invoke(string url, string token = null, InvokeFunctionOptions options = null)
33+
public async Task<string> Invoke(string url, string? token = null, InvokeFunctionOptions? options = null)
3334
{
3435
var response = await HandleRequest(url, token, options);
3536

@@ -44,7 +45,7 @@ public static async Task<string> Invoke(string url, string token = null, InvokeF
4445
/// <param name="token">Anon Key.</param>
4546
/// <param name="options">Options</param>
4647
/// <returns></returns>
47-
public static async Task<T> Invoke<T>(string url, string token = null, InvokeFunctionOptions options = null)
48+
public async Task<T?> Invoke<T>(string url, string? token = null, InvokeFunctionOptions? options = null) where T : class
4849
{
4950
var response = await HandleRequest(url, token, options);
5051

@@ -61,7 +62,7 @@ public static async Task<T> Invoke<T>(string url, string token = null, InvokeFun
6162
/// <param name="options"></param>
6263
/// <returns></returns>
6364
/// <exception cref="RequestException"></exception>
64-
private static async Task<HttpResponseMessage> HandleRequest(string url, string token = null, InvokeFunctionOptions options = null)
65+
private async Task<HttpResponseMessage> HandleRequest(string url, string? token = null, InvokeFunctionOptions? options = null)
6566
{
6667
if (options == null)
6768
{

Diff for: Functions/Functions.csproj

+34-29
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
3-
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
5-
<PackOnBuild>true</PackOnBuild>
6-
<PackageId>functions-csharp</PackageId>
7-
<Authors>Joseph Schultz &lt;[email protected]&gt;</Authors>
8-
<Copyright>MIT</Copyright>
9-
<NeutralLanguage>en</NeutralLanguage>
10-
<Owners>Joseph Schultz &lt;[email protected]&gt;</Owners>
11-
<Summary>A C# client for Supabase functions</Summary>
12-
<Title>Function</Title>
13-
<Description>A C# client for Supabase functions</Description>
14-
<RootNamespace>Supabase.Functions</RootNamespace>
15-
<PackageIconUrl>https://avatars.githubusercontent.com/u/54469796?s=200&amp;v=4</PackageIconUrl>
16-
<PackageLicenseUrl>https://github.com/supabase-community/functions-csharp/blob/master/LICENSE</PackageLicenseUrl>
17-
<PackageProjectUrl>https://github.com/supabase-community/functions-csharp</PackageProjectUrl>
18-
<PackageTags>supabase, functions</PackageTags>
19-
<PackageVersion>1.0.1</PackageVersion>
20-
<ReleaseVersion>1.0.1</ReleaseVersion>
21-
</PropertyGroup>
22-
<PropertyGroup Condition=" '$(Version)' == '' ">
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<PackOnBuild>true</PackOnBuild>
6+
<PackageId>functions-csharp</PackageId>
7+
<Authors>Joseph Schultz &lt;[email protected]&gt;</Authors>
8+
<Copyright>MIT</Copyright>
9+
<NeutralLanguage>en</NeutralLanguage>
10+
<Owners>Joseph Schultz &lt;[email protected]&gt;</Owners>
11+
<Summary>A C# client for Supabase functions</Summary>
12+
<Title>Function</Title>
13+
<Description>A C# client for Supabase functions</Description>
14+
<RootNamespace>Supabase.Functions</RootNamespace>
15+
<PackageIconUrl>https://avatars.githubusercontent.com/u/54469796?s=200&amp;v=4</PackageIconUrl>
16+
<PackageLicenseUrl>https://github.com/supabase-community/functions-csharp/blob/master/LICENSE</PackageLicenseUrl>
17+
<PackageProjectUrl>https://github.com/supabase-community/functions-csharp</PackageProjectUrl>
18+
<PackageTags>supabase, functions</PackageTags>
19+
<PackageVersion>1.1.0</PackageVersion>
20+
<ReleaseVersion>1.1.0</ReleaseVersion>
21+
</PropertyGroup>
22+
<PropertyGroup>
23+
<Nullable>enable</Nullable>
24+
<LangVersion>8.0</LangVersion>
25+
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Version)' == '' ">
2328
<VersionPrefix Condition=" '$(VersionPrefix)' == '' ">1.0.1</VersionPrefix>
2429
<VersionSuffix Condition=" '$(VersionSuffix)' == '' ">
2530
</VersionSuffix>
2631
<Version Condition=" '$(VersionSuffix)' != '' ">$(VersionPrefix)-$(VersionSuffix)</Version>
2732
<Version Condition=" '$(Version)' == '' ">$(VersionPrefix)</Version>
2833
</PropertyGroup>
29-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
30-
<LangVersion>8.0</LangVersion>
31-
</PropertyGroup>
32-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
33-
<LangVersion>8.0</LangVersion>
34-
</PropertyGroup>
35-
<ItemGroup>
36-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
37-
</ItemGroup>
34+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
35+
<LangVersion>8.0</LangVersion>
36+
</PropertyGroup>
37+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
38+
<LangVersion>8.0</LangVersion>
39+
</PropertyGroup>
40+
<ItemGroup>
41+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
42+
</ItemGroup>
3843
</Project>

Diff for: Functions/Interfaces/IFunctionsClient.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Net.Http;
2+
using System.Threading.Tasks;
3+
4+
namespace Supabase.Functions.Interfaces
5+
{
6+
public interface IFunctionsClient
7+
{
8+
Task<string> Invoke(string url, string? token = null, Client.InvokeFunctionOptions? options = null);
9+
Task<T?> Invoke<T>(string url, string? token = null, Client.InvokeFunctionOptions? options = null) where T : class;
10+
Task<HttpContent> RawInvoke(string url, string? token = null, Client.InvokeFunctionOptions? options = null);
11+
}
12+
}

Diff for: Functions/Responses/BaseResponse.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace Supabase.Functions.Responses
1010
public class BaseResponse
1111
{
1212
[JsonIgnore]
13-
public HttpResponseMessage ResponseMessage { get; set; }
13+
public HttpResponseMessage? ResponseMessage { get; set; }
1414

1515
[JsonIgnore]
16-
public string Content { get; set; }
16+
public string? Content { get; set; }
1717
}
1818
}

Diff for: Functions/Responses/ErrorResponse.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ namespace Supabase.Functions.Responses
88
/// </summary>
99
public class ErrorResponse : BaseResponse
1010
{
11-
public string Message { get; set; }
11+
public string? Message { get; set; }
1212
}
1313
}
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,57 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IdentityModel.Tokens.Jwt;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
using Microsoft.IdentityModel.Tokens;
8-
using Microsoft.VisualStudio.TestTools.UnitTesting;
9-
using static Supabase.Functions.Client;
10-
11-
namespace FunctionsTests
12-
{
13-
[TestClass]
14-
public class Client
15-
{
16-
[TestMethod("Invokes a function.")]
17-
public async Task Invokes()
18-
{
19-
var token = Environment.GetEnvironmentVariable("TOKEN");
20-
var endpoint = Environment.GetEnvironmentVariable("FUNCTION_ENDPOINT");
21-
22-
var result = await Invoke(endpoint, token, new InvokeFunctionOptions
23-
{
24-
Body = new Dictionary<string, object>
25-
{
26-
{"name", "supabase" }
27-
}
28-
});
29-
30-
Assert.IsTrue(result.Contains("supabase"));
31-
32-
33-
var result2 = await Invoke<Dictionary<string, string>>(endpoint, token, new InvokeFunctionOptions
34-
{
35-
Body = new Dictionary<string, object>
36-
{
37-
{ "name", "functions" }
38-
}
39-
});
40-
41-
Assert.IsInstanceOfType(result2, typeof(Dictionary<string, string>));
42-
Assert.IsTrue(result2.ContainsKey("message"));
43-
Assert.IsTrue(result2["message"].Contains("functions"));
44-
45-
46-
var result3 = await RawInvoke(endpoint, token, new InvokeFunctionOptions
47-
{
48-
Body = new Dictionary<string, object>
49-
{
50-
{ "name", "functions" }
51-
}
52-
});
53-
54-
var bytes = await result3.ReadAsByteArrayAsync();
55-
56-
Assert.IsInstanceOfType(bytes, typeof(byte[]));
57-
}
58-
}
59-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Supabase.Functions;
6+
using static Supabase.Functions.Client;
7+
8+
namespace FunctionsTests
9+
{
10+
[TestClass]
11+
public class ClientTests
12+
{
13+
[TestMethod("Invokes a function.")]
14+
public async Task Invokes()
15+
{
16+
var token = Environment.GetEnvironmentVariable("TOKEN");
17+
var endpoint = Environment.GetEnvironmentVariable("FUNCTION_ENDPOINT");
18+
var client = new Client();
19+
20+
var result = await client.Invoke(endpoint, token, new InvokeFunctionOptions
21+
{
22+
Body = new Dictionary<string, object>
23+
{
24+
{"name", "supabase" }
25+
}
26+
});
27+
28+
Assert.IsTrue(result.Contains("supabase"));
29+
30+
31+
var result2 = await client.Invoke<Dictionary<string, string>>(endpoint, token, new InvokeFunctionOptions
32+
{
33+
Body = new Dictionary<string, object>
34+
{
35+
{ "name", "functions" }
36+
}
37+
});
38+
39+
Assert.IsInstanceOfType(result2, typeof(Dictionary<string, string>));
40+
Assert.IsTrue(result2.ContainsKey("message"));
41+
Assert.IsTrue(result2["message"].Contains("functions"));
42+
43+
44+
var result3 = await client.RawInvoke(endpoint, token, new InvokeFunctionOptions
45+
{
46+
Body = new Dictionary<string, object>
47+
{
48+
{ "name", "functions" }
49+
}
50+
});
51+
52+
var bytes = await result3.ReadAsByteArrayAsync();
53+
54+
Assert.IsInstanceOfType(bytes, typeof(byte[]));
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)