aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/fortee/fortee.go
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-08-17 19:35:47 +0900
committernsfisis <nsfisis@gmail.com>2024-08-17 19:46:43 +0900
commit34592a45efd9ceb0579c8eae1ba752da7f625950 (patch)
tree8920a4db5cf1c97b719b959030d6089cc545d212 /backend/fortee/fortee.go
parentf926ef682de637b717d3b0cc0eaee43c59e83c95 (diff)
downloadphperkaigi-2025-albatross-34592a45efd9ceb0579c8eae1ba752da7f625950.tar.gz
phperkaigi-2025-albatross-34592a45efd9ceb0579c8eae1ba752da7f625950.tar.zst
phperkaigi-2025-albatross-34592a45efd9ceb0579c8eae1ba752da7f625950.zip
refactor(backend): move fortee package
Diffstat (limited to 'backend/fortee/fortee.go')
-rw-r--r--backend/fortee/fortee.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/backend/fortee/fortee.go b/backend/fortee/fortee.go
new file mode 100644
index 0000000..5ec7963
--- /dev/null
+++ b/backend/fortee/fortee.go
@@ -0,0 +1,46 @@
+package fortee
+
+import (
+ "context"
+ "errors"
+ "net/http"
+)
+
+const (
+ apiEndpoint = "https://fortee.jp"
+)
+
+var (
+ ErrLoginFailed = errors.New("fortee login failed")
+)
+
+func Login(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
+}