Skip to content

feat: Allow to specify the http method when call invoke #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
11 changes: 9 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ jobs:
- name: Build
run: dotnet build --configuration Release --no-restore

- name: Initialize Testing Stack
run: docker-compose up -d
- uses: supabase/setup-cli@v1
with:
version: latest

- name: Start supabase
run: supabase start

# - name: Initialize Testing Stack
# run: docker-compose up -d

- name: Test
run: dotnet test --no-restore
21 changes: 21 additions & 0 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
on:
push:
branches:
- master
- release/*

permissions:
contents: write
pull-requests: write

name: release-please

jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: googleapis/release-please-action@v4
with:
target-branch: ${{ github.ref_name }}
manifest-file: .release-please-manifest.json
config-file: release-please-config.json
15 changes: 11 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
name: Publish NuGet Package

on:
push:
branches:
- release/* # Default release branch
workflow_run:
workflows: ["release-please"]
branches: [master]
types:
- completed

# push:
# branches:
# - release/* # Default release branch

jobs:
publish:
if: ${{ github.repository_owner == 'supabase' && github.event.workflow_run.conclusion == 'success' }}
name: build, pack & publish
runs-on: ubuntu-latest
steps:
Expand All @@ -24,7 +31,7 @@ jobs:
check-name: build-and-test
repo-token: ${{ secrets.GITHUB_TOKEN }}
wait-interval: 10

- name: Restore dependencies
run: dotnet restore

Expand Down
3 changes: 3 additions & 0 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
".": "2.0.0"
}
65 changes: 43 additions & 22 deletions Functions/Client.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using Newtonsoft.Json;
using Supabase.Core;
using Supabase.Core.Extensions;
using Supabase.Functions.Interfaces;
using Supabase.Functions.Responses;
using System;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using Supabase.Core;
using Supabase.Core.Extensions;
using Supabase.Functions.Exceptions;
using Supabase.Functions.Interfaces;
using Supabase.Functions.Responses;

[assembly: InternalsVisibleTo("FunctionsTests")]

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

/// <summary>
/// Function that can be set to return dynamic headers.
///
///
/// Headers specified in the method parameters will ALWAYS take precedence over headers returned by this function.
/// </summary>
public Func<Dictionary<string, string>>? GetHeaders { get; set; }
Expand All @@ -45,8 +45,11 @@ public Client(string baseUrl)
/// <param name="token">Anon Key.</param>
/// <param name="options">Options</param>
/// <returns></returns>
public async Task<HttpContent> RawInvoke(string functionName, string? token = null,
InvokeFunctionOptions? options = null)
public async Task<HttpContent> RawInvoke(
string functionName,
string? token = null,
InvokeFunctionOptions? options = null
)
{
var url = $"{_baseUrl}/{functionName}";

Expand All @@ -60,8 +63,11 @@ public async Task<HttpContent> RawInvoke(string functionName, string? token = nu
/// <param name="token">Anon Key.</param>
/// <param name="options">Options</param>
/// <returns></returns>
public async Task<string> Invoke(string functionName, string? token = null,
InvokeFunctionOptions? options = null)
public async Task<string> Invoke(
string functionName,
string? token = null,
InvokeFunctionOptions? options = null
)
{
var url = $"{_baseUrl}/{functionName}";
var response = await HandleRequest(url, token, options);
Expand All @@ -77,8 +83,12 @@ public async Task<string> Invoke(string functionName, string? token = null,
/// <param name="token">Anon Key.</param>
/// <param name="options">Options</param>
/// <returns></returns>
public async Task<T?> Invoke<T>(string functionName, string? token = null,
InvokeFunctionOptions? options = null) where T : class
public async Task<T?> Invoke<T>(
string functionName,
string? token = null,
InvokeFunctionOptions? options = null
)
where T : class
{
var url = $"{_baseUrl}/{functionName}";
var response = await HandleRequest(url, token, options);
Expand All @@ -96,8 +106,11 @@ public async Task<string> Invoke(string functionName, string? token = null,
/// <param name="options"></param>
/// <returns></returns>
/// <exception cref="FunctionsException"></exception>
private async Task<HttpResponseMessage> HandleRequest(string url, string? token = null,
InvokeFunctionOptions? options = null)
private async Task<HttpResponseMessage> HandleRequest(
string url,
string? token = null,
InvokeFunctionOptions? options = null
)
{
options ??= new InvokeFunctionOptions();

Expand All @@ -113,26 +126,34 @@ private async Task<HttpResponseMessage> HandleRequest(string url, string? token

options.Headers["X-Client-Info"] = Util.GetAssemblyVersion(typeof(Client));

if (options.FunctionRegion != FunctionRegion.Any)
{
options.Headers["x-region"] = options.FunctionRegion.ToString();
}

var builder = new UriBuilder(url);
var query = HttpUtility.ParseQueryString(builder.Query);

builder.Query = query.ToString();

using var requestMessage = new HttpRequestMessage(HttpMethod.Post, builder.Uri);
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(options.Body), Encoding.UTF8,
"application/json");
using var requestMessage = new HttpRequestMessage(options.HttpMethod, builder.Uri);
requestMessage.Content = new StringContent(
JsonConvert.SerializeObject(options.Body),
Encoding.UTF8,
"application/json"
);

foreach (var kvp in options.Headers)
{
requestMessage.Headers.TryAddWithoutValidation(kvp.Key, kvp.Value);
}

if (_httpClient.Timeout != options.HttpTimeout)
{
_httpClient = new HttpClient();
_httpClient.Timeout = options.HttpTimeout;
}

var response = await _httpClient.SendAsync(requestMessage);

if (response.IsSuccessStatusCode && !response.Headers.Contains("x-relay-error"))
Expand All @@ -143,10 +164,10 @@ private async Task<HttpResponseMessage> HandleRequest(string url, string? token
{
Content = content,
Response = response,
StatusCode = (int)response.StatusCode
StatusCode = (int)response.StatusCode,
};
exception.AddReason();
throw exception;
}
}
}
}
6 changes: 4 additions & 2 deletions Functions/Functions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
<PackageIconUrl>https://avatars.githubusercontent.com/u/54469796?s=200&amp;v=4</PackageIconUrl>
<PackageProjectUrl>https://github.com/supabase-community/functions-csharp</PackageProjectUrl>
<PackageTags>supabase, functions</PackageTags>
<!-- x-release-please-start-version -->
<PackageVersion>2.0.0</PackageVersion>
<ReleaseVersion>2.0.0</ReleaseVersion>
<!-- x-release-please-end -->
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand All @@ -31,7 +33,7 @@
</PropertyGroup>

<PropertyGroup Condition=" '$(Version)' == '' ">
<VersionPrefix Condition=" '$(VersionPrefix)' == '' ">2.0.0</VersionPrefix>
<VersionPrefix Condition=" '$(VersionPrefix)' == '' ">2.0.0</VersionPrefix> <!-- x-release-please-version -->
<VersionSuffix Condition=" '$(VersionSuffix)' == '' "></VersionSuffix>
<Version Condition=" '$(VersionSuffix)' != '' ">$(VersionPrefix)-$(VersionSuffix)</Version>
<Version Condition=" '$(Version)' == '' ">$(VersionPrefix)</Version>
Expand All @@ -46,4 +48,4 @@
<None Include="..\.github\icon.png" Pack="true" Link="icon.png" PackagePath="\" />
<None Include="..\README.md" Pack="true" Link="README.md" PackagePath="\" />
</ItemGroup>
</Project>
</Project>
Loading