Debian Codegraph @

clientprovider.go

raw symbols SCIP
1 package g8scip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/
2
3 import (
4 "timescip-go gomod github.com/golang/go/src go1.23.3 time/"
5
6 "github.com/TwiN/gocache/v2scip-go gomod github.com/TwiN/gocache/v2 v2.2.2 `github.com/TwiN/gocache/v2`/"
7 )
8
9 // ClientProvider has the task of retrieving a Client from an external source (e.g. a database) when provided with a
10 // token. It should be used when you have a lot of tokens, and it wouldn't make sense to register all of them using
11 // AuthorizationService's WithToken, WithTokens, WithClient or WithClients.
12 //
13 // Note that the provider is used as a fallback source. As such, if a token is explicitly registered using one of the 4
14 // aforementioned functions, the client provider will not be used by the AuthorizationService when a request is made
15 // with said token. It will, however, be called upon if a token that is not explicitly registered in
16 // AuthorizationService is sent alongside a request going through the Gate.
17 //
18 // clientProvider := g8.NewClientProvider(func(token string) *g8.Client {
19 // // We'll assume that the following function calls your database and returns a struct "User" that
20 // // has the user's token as well as the permissions granted to said user
21 // user := database.GetUserByToken(token)
22 // if user != nil {
23 // return g8.NewClient(user.Token).WithPermissions(user.Permissions)
24 // }
25 // return nil
26 // })
27 // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClientProvider(clientProvider))
28 type ClientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider# struct {
29 getClientByTokenFuncscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#getClientByTokenFunc. func(token string) *Clientscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#
30
31 cachescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#cache. Cachescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Cache#
32 }
33
34 // NewClientProvider creates a ClientProvider
35 // The parameter that must be passed is a function that the provider will use to retrieve a client by a given token
36 //
37 // Example:
38 //
39 // clientProvider := g8.NewClientProvider(func(token string) *g8.Client {
40 // // We'll assume that the following function calls your database and returns a struct "User" that
41 // // has the user's token as well as the permissions granted to said user
42 // user := database.GetUserByToken(token)
43 // if user == nil {
44 // return nil
45 // }
46 // return g8.NewClient(user.Token).WithPermissions(user.Permissions)
47 // })
48 // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClientProvider(clientProvider))
49 func NewClientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/NewClientProvider().(getClientByTokenFunc func(token string) *Clientscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#) *ClientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider# {
50 return &ClientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#{
51 getClientByTokenFuncscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#getClientByTokenFunc.: getClientByTokenFunc,
52 }
53 }
54
55 // WithCache enables an in-memory cache for the ClientProvider.
56 //
57 // Example:
58 //
59 // clientProvider := g8.NewClientProvider(func(token string) *g8.Client {
60 // // We'll assume that the following function calls your database and returns a struct "User" that
61 // // has the user's token as well as the permissions granted to said user
62 // user := database.GetUserByToken(token)
63 // if user != nil {
64 // return g8.NewClient(user.Token).WithPermissions(user.Permissions)
65 // }
66 // return nil
67 // })
68 // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClientProvider(clientProvider.WithCache(time.Hour, 70000)))
69 func (provider *ClientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#) WithCachescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#WithCache().(ttl timescip-go gomod github.com/golang/go/src go1.23.3 time/.Durationscip-go gomod github.com/golang/go/src go1.23.3 time/Duration#, maxSize int) *ClientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider# {
70 return provider.WithCustomCachescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#WithCustomCache().(
71 gocachescip-go gomod github.com/TwiN/gocache/v2 v2.2.2 `github.com/TwiN/gocache/v2`/.NewCachescip-go gomod github.com/TwiN/gocache/v2 v2.2.2 `github.com/TwiN/gocache/v2`/NewCache().().WithEvictionPolicyscip-go gomod github.com/TwiN/gocache/v2 v2.2.2 `github.com/TwiN/gocache/v2`/Cache#WithEvictionPolicy().(gocachescip-go gomod github.com/TwiN/gocache/v2 v2.2.2 `github.com/TwiN/gocache/v2`/.LeastRecentlyUsedscip-go gomod github.com/TwiN/gocache/v2 v2.2.2 `github.com/TwiN/gocache/v2`/LeastRecentlyUsed.).WithMaxSizescip-go gomod github.com/TwiN/gocache/v2 v2.2.2 `github.com/TwiN/gocache/v2`/Cache#WithMaxSize().(maxSize).WithDefaultTTLscip-go gomod github.com/TwiN/gocache/v2 v2.2.2 `github.com/TwiN/gocache/v2`/Cache#WithDefaultTTL().(ttl),
72 )
73 }
74
75 // WithCustomCache allows you to use a custom cache implementation instead of the default one.
76 // By default, using WithCache will leverage gocache.
77 //
78 // Note that the custom cache must implement the Cache interface
79 func (provider *ClientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#) WithCustomCachescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#WithCustomCache().(cache Cachescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Cache#) *ClientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider# {
80 provider.cachescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#cache. = cache
81 return provider
82 }
83
84 // GetClientByToken retrieves a client by its token through the provided getClientByTokenFunc.
85 func (provider *ClientProviderscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#) GetClientByTokenscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#GetClientByToken().(token string) *Clientscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client# {
86 if provider.cachescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#cache. == nil {
87 return provider.getClientByTokenFuncscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#getClientByTokenFunc.(token)
88 }
89 if cachedClient, exists := provider.cachescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#cache..Getscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Cache#Get.(token); exists {
90 if cachedClient == nil {
91 return nil
92 }
93 // Safely typecast the client.
94 // Regardless of whether the typecast is successful or not, we return client since it'll be either client or
95 // nil. Technically, it should never be nil, but it's better to be safe than sorry.
96 client, _ := cachedClient.(*Clientscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Client#)
97 return client
98 }
99 client := provider.getClientByTokenFuncscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#getClientByTokenFunc.(token)
100 provider.cachescip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ClientProvider#cache..Setscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/Cache#Set.(token, client)
101 return client
102 }