Пример #1
0
 /// <summary>
 /// Initializes a Supabase Client.
 /// </summary>
 /// <param name="supabaseUrl"></param>
 /// <param name="supabaseKey"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static void Initialize(string supabaseUrl, string supabaseKey, SupabaseOptions options = null, Action <Client> callback = null)
 {
     Task.Run(async() =>
     {
         var result = await InitializeAsync(supabaseUrl, supabaseKey, options);
         callback?.Invoke(result);
     });
 }
Пример #2
0
        /// <summary>
        /// Runs a remote procedure.
        /// </summary>
        /// <param name="procedureName"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static Task <BaseResponse> Rpc(string supabaseUrl, string supabaseKey, string procedureName, Dictionary <string, object> parameters, SupabaseOptions options = null)
        {
            if (options == null)
            {
                options = new SupabaseOptions();
            }

            return(Postgrest.StatelessClient.Rpc(procedureName, parameters, GetRestOptions(supabaseUrl, supabaseKey, options)));
        }
Пример #3
0
        /// <summary>
        /// Supabase Storage allows you to manage user-generated content, such as photos or videos.
        /// </summary>
        /// <param name="supabaseUrl"></param>
        /// <param name="supabaseKey"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static Storage.Client Storage(string supabaseUrl, string supabaseKey = null, SupabaseOptions options = null)
        {
            if (options == null)
            {
                options = new SupabaseOptions();
            }

            var headers = GetAuthHeaders(supabaseKey, options).MergeLeft(options.Headers);

            return(new Storage.Client(string.Format(options.StorageUrlFormat, supabaseUrl), headers));
        }
Пример #4
0
        public static Postgrest.StatelessClientOptions GetRestOptions(string supabaseUrl, string supabaseKey = null, SupabaseOptions options = null)
        {
            if (options == null)
            {
                options = new SupabaseOptions();
            }

            var headers = GetAuthHeaders(supabaseKey, options).MergeLeft(options.Headers);

            return(new Postgrest.StatelessClientOptions(string.Format(options.RestUrlFormat, supabaseUrl))
            {
                Schema = options.Schema,
                Headers = headers
            });
        }
Пример #5
0
        public static Gotrue.StatelessClient.StatelessClientOptions GetAuthOptions(string supabaseUrl, string supabaseKey = null, SupabaseOptions options = null)
        {
            if (options == null)
            {
                options = new SupabaseOptions();
            }

            var headers = GetAuthHeaders(supabaseKey, options).MergeLeft(options.Headers);

            return(new Gotrue.StatelessClient.StatelessClientOptions
            {
                Url = string.Format(options.AuthUrlFormat, supabaseUrl),
                Headers = headers
            });
        }
Пример #6
0
        /// <summary>
        /// Gets the Postgrest client to prepare for a query.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static SupabaseTable <T> From <T>(string supabaseUrl, string supabaseKey, SupabaseOptions options = null) where T : BaseModel, new()
        {
            if (options == null)
            {
                options = new SupabaseOptions();
            }

            var headers = GetAuthHeaders(supabaseKey, options).MergeLeft(options.Headers);


            return(new SupabaseTable <T>(string.Format(options.RestUrlFormat, supabaseUrl), new Postgrest.ClientOptions
            {
                Headers = headers,
                Schema = options.Schema
            }));
        }
Пример #7
0
        /// <summary>
        /// Initializes a Supabase Client Asynchronously.
        /// </summary>
        /// <param name="supabaseUrl"></param>
        /// <param name="supabaseKey"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static async Task <Client> InitializeAsync(string supabaseUrl, string supabaseKey, SupabaseOptions options = null)
        {
            instance = new Client();

            instance.SupabaseUrl = supabaseUrl;
            instance.SupabaseKey = supabaseKey;

            if (options == null)
            {
                options = new SupabaseOptions();
            }

            instance.options     = options;
            instance.AuthUrl     = string.Format(options.AuthUrlFormat, supabaseUrl);
            instance.RestUrl     = string.Format(options.RestUrlFormat, supabaseUrl);
            instance.RealtimeUrl = string.Format(options.RealtimeUrlFormat, supabaseUrl).Replace("http", "ws");
            instance.StorageUrl  = string.Format(options.StorageUrlFormat, supabaseUrl);
            instance.Schema      = options.Schema;

            instance.Auth = await Gotrue.Client.InitializeAsync(new Gotrue.ClientOptions
            {
                Url              = instance.AuthUrl,
                Headers          = instance.GetAuthHeaders(),
                AutoRefreshToken = options.AutoRefreshToken,
                PersistSession   = options.PersistSession,
                SessionDestroyer = options.SessionDestroyer,
                SessionPersistor = options.SessionPersistor,
                SessionRetriever = options.SessionRetriever
            });

            instance.Auth.StateChanged += Auth_StateChanged;

            if (options.ShouldInitializeRealtime)
            {
                instance.Realtime = Supabase.Realtime.Client.Initialize(instance.RealtimeUrl, new Realtime.ClientOptions
                {
                    Parameters = { ApiKey = instance.SupabaseKey }
                });

                if (options.AutoConnectRealtime)
                {
                    await instance.Realtime.ConnectAsync();
                }
            }

            return(instance);
        }
Пример #8
0
        internal static Dictionary <string, string> GetAuthHeaders(string supabaseKey, SupabaseOptions options)
        {
            var headers = new Dictionary <string, string>();

            headers["apiKey"]        = supabaseKey;
            headers["X-Client-Info"] = Util.GetAssemblyVersion();

            // In Regard To: https://github.com/supabase/supabase-csharp/issues/5
            if (options.Headers.ContainsKey("Authorization"))
            {
                headers["Authorization"] = options.Headers["Authorization"];
            }
            else
            {
                var bearer = supabaseKey;
                headers["Authorization"] = $"Bearer {bearer}";
            }

            return(headers);
        }