Newer
Older
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
package mytokenlib
import (
"github.com/zachmann/mytoken/internal/httpClient"
"github.com/zachmann/mytoken/internal/server/endpoints/configuration/pkg"
"github.com/zachmann/mytoken/internal/utils"
"github.com/zachmann/mytoken/pkg/model"
)
type Mytoken struct {
pkg.MytokenConfiguration
}
func NewMytokenInstance(url string) (*Mytoken, error) {
configEndpoint := utils.CombineURLPath(url, "/.well-known/mytoken-configuration")
resp, err := httpClient.Do().R().SetResult(&pkg.MytokenConfiguration{}).SetError(&model.APIError{}).Get(configEndpoint)
if err != nil {
return nil, newMytokenErrorFromError("could not connect to mytoken instance", err)
}
if e := resp.Error(); e != nil {
if errRes := e.(*model.APIError); errRes != nil && len(errRes.Error) > 0 {
return nil, &MytokenError{
err: errRes.Error,
errorDetails: errRes.ErrorDescription,
}
}
}
config, ok := resp.Result().(*pkg.MytokenConfiguration)
if !ok {
return nil, &MytokenError{
err: "unexpected response from mytoken server",
}
}
return &Mytoken{
MytokenConfiguration: *config,
}, nil
}