aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/fortee/fortee.go
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-08-17 21:11:07 +0900
committernsfisis <nsfisis@gmail.com>2024-08-17 21:11:07 +0900
commit01d3120fd7129f573d88f7aa7c227b3ef93fe368 (patch)
treeeea4f5ac0f025c7bdbff71d2ee83b4dce99355ef /backend/fortee/fortee.go
parentf926ef682de637b717d3b0cc0eaee43c59e83c95 (diff)
parentb923a9d6534820d33f42bc65c47ae22889bde922 (diff)
downloadiosdc-japan-2024-albatross-01d3120fd7129f573d88f7aa7c227b3ef93fe368.tar.gz
iosdc-japan-2024-albatross-01d3120fd7129f573d88f7aa7c227b3ef93fe368.tar.zst
iosdc-japan-2024-albatross-01d3120fd7129f573d88f7aa7c227b3ef93fe368.zip
Merge branch 'feat/icon'
Diffstat (limited to 'backend/fortee/fortee.go')
-rw-r--r--backend/fortee/fortee.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/backend/fortee/fortee.go b/backend/fortee/fortee.go
new file mode 100644
index 0000000..cfe4eff
--- /dev/null
+++ b/backend/fortee/fortee.go
@@ -0,0 +1,62 @@
+package fortee
+
+import (
+ "context"
+ "errors"
+ "net/http"
+)
+
+const (
+ Endpoint = "https://fortee.jp"
+)
+
+var (
+ ErrLoginFailed = errors.New("fortee login failed")
+ ErrUserNotFound = errors.New("fortee user not found")
+)
+
+func Login(ctx context.Context, username string, password string) (string, error) {
+ client, err := NewClientWithResponses(Endpoint, 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
+}
+
+func GetUserAvatarURL(ctx context.Context, username string) (string, error) {
+ client, err := NewClientWithResponses(Endpoint, WithRequestEditorFn(addAcceptHeader))
+ if err != nil {
+ return "", err
+ }
+ res, err := client.GetUserWithResponse(ctx, username)
+ if err != nil {
+ return "", err
+ }
+ if res.StatusCode() != http.StatusOK {
+ return "", ErrUserNotFound
+ }
+ return res.JSON200.AvatarURL, nil
+}
+
+// fortee API denies requests without Accept header.
+func addAcceptHeader(_ context.Context, req *http.Request) error {
+ req.Header.Set("Accept", "application/json")
+ return nil
+}