From 397bf28ac68bcf4ea94a4a189ee9ae0894dc5394 Mon Sep 17 00:00:00 2001 From: Vladislav Yarmak Date: Tue, 30 Mar 2021 15:51:20 +0300 Subject: [PATCH] extend SE API with login method --- seclient/messages.go | 2 ++ seclient/seclient.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/seclient/messages.go b/seclient/messages.go index 48d2331..7007f61 100644 --- a/seclient/messages.go +++ b/seclient/messages.go @@ -94,3 +94,5 @@ type SEDiscoverResponse struct { } `json:"data"` Status SEStatusPair `json:"return_code"` } + +type SESubscriberLoginResponse SERegisterSubscriberResponse diff --git a/seclient/seclient.go b/seclient/seclient.go index 929d9fa..ed0b8a1 100644 --- a/seclient/seclient.go +++ b/seclient/seclient.go @@ -291,6 +291,49 @@ func (c *SEClient) Discover(ctx context.Context, requestedGeo string) ([]SEIPEnt return discoverRes.Data.IPs, nil } +func (c *SEClient) Login(ctx context.Context) error { + loginInput := url.Values{ + "login": {c.SubscriberEmail}, + "password": {c.SubscriberPassword}, + "client_type": {c.Settings.ClientType}, + } + req, err := http.NewRequestWithContext( + ctx, + "POST", + c.Settings.Endpoints.SubscriberLogin, + strings.NewReader(loginInput.Encode()), + ) + if err != nil { + return err + } + c.populateRequest(req) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Header.Set("Accept", "application/json") + + resp, err := c.HttpClient.Do(req) + if err != nil { + return err + } + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("bad http status: %s", resp.Status) + } + + decoder := json.NewDecoder(resp.Body) + var loginRes SESubscriberLoginResponse + err = decoder.Decode(&loginRes) + cleanupBody(resp.Body) + if err != nil { + return err + } + + if loginRes.Status.Code != SE_STATUS_OK { + return fmt.Errorf("API responded with error message: code=%d, msg=\"%s\"", + loginRes.Status.Code, loginRes.Status.Message) + } + return nil +} + func (c *SEClient) GetProxyCredentials() (string, string) { return c.AssignedDeviceIDHash, c.DevicePassword }