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
|
package fortee
import (
"context"
"errors"
"net/http"
)
const (
apiEndpoint = "https://fortee.jp"
)
var (
ErrLoginFailed = errors.New("fortee login failed")
)
func LoginFortee(ctx context.Context, username string, password string) (string, error) {
client, err := NewClientWithResponses(apiEndpoint, WithRequestEditorFn(addAcceptHeader))
if err != nil {
return "", err
}
res, err := client.PostLoginWithFormdataBodyWithResponse(ctx, PostLoginFormdataRequestBody{
Username: username,
Password: password,
})
if err != nil {
return "", err
}
if res.StatusCode() != http.StatusOK {
return "", ErrLoginFailed
}
resOk := res.JSON200
if !resOk.LoggedIn {
return "", ErrLoginFailed
}
if resOk.User == nil {
return "", ErrLoginFailed
}
return resOk.User.Username, nil
}
// fortee API denies requests without Accept header.
func addAcceptHeader(_ context.Context, req *http.Request) error {
req.Header.Set("Accept", "application/json")
return nil
}
|