-
Notifications
You must be signed in to change notification settings - Fork 8
/
providers.go
76 lines (62 loc) · 1.74 KB
/
providers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package judge0
import (
"fmt"
"net/http"
)
// AuthProvider interface is used to set the authentication headers of all the requests
type AuthProvider interface {
SetAuthHeaders(req *http.Request) // Set the Authentication Headers
GetBaseURL() string // Get the Base URL of the request
}
// Implements the AuthProivder Interface for the RapidAPI Judge0 Provider
type rapidAPIProvider struct {
rapidAPIKey string
}
func (rap *rapidAPIProvider) SetAuthHeaders(req *http.Request) {
req.Header.Set("x-rapidapi-key", rap.rapidAPIKey)
req.Header.Set("x-rapidapi-host", rapidAPIHost)
}
func (rap *rapidAPIProvider) GetBaseURL() string {
return rapidAPIHost
}
// Implements the AuthProvider Interface for the Sulu Judge0 Provider
type suluAPIProvider struct {
suluAPIKey string
}
func (sap *suluAPIProvider) SetAuthHeaders(req *http.Request) {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", sap.suluAPIKey))
}
func (sap *suluAPIProvider) GetBaseURL() string {
return suluAPIHost
}
type customAPIProvider struct {
baseURL string
headerField string
customAPIKey string
}
func (cap *customAPIProvider) SetAuthHeaders(req *http.Request) {
req.Header.Set(cap.headerField, cap.customAPIKey)
}
func (cap *customAPIProvider) GetBaseURL() string {
return cap.baseURL
}
func NewCustomProvider(apiKey string, baseUrl string, headerField string) AuthProvider {
if headerField == "" {
headerField = "X-Auth-Token"
}
return &customAPIProvider{
baseURL: baseUrl,
headerField: headerField,
customAPIKey: apiKey,
}
}
func NewSuluProvider(apiKey string) AuthProvider {
return &suluAPIProvider{
suluAPIKey: apiKey,
}
}
func NewRapidAPIProvider(apiKey string) AuthProvider {
return &rapidAPIProvider{
rapidAPIKey: apiKey,
}
}