extend SE API with login method

This commit is contained in:
Vladislav Yarmak
2021-03-30 15:51:20 +03:00
parent 6dc7d79f87
commit 397bf28ac6
2 changed files with 45 additions and 0 deletions
+2
View File
@@ -94,3 +94,5 @@ type SEDiscoverResponse struct {
} `json:"data"` } `json:"data"`
Status SEStatusPair `json:"return_code"` Status SEStatusPair `json:"return_code"`
} }
type SESubscriberLoginResponse SERegisterSubscriberResponse
+43
View File
@@ -291,6 +291,49 @@ func (c *SEClient) Discover(ctx context.Context, requestedGeo string) ([]SEIPEnt
return discoverRes.Data.IPs, nil 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) { func (c *SEClient) GetProxyCredentials() (string, string) {
return c.AssignedDeviceIDHash, c.DevicePassword return c.AssignedDeviceIDHash, c.DevicePassword
} }