wndclient: preparations for first request

This commit is contained in:
Vladislav Yarmak
2021-06-23 20:38:08 +03:00
parent 58b7c53ef0
commit 9a391d6e92
2 changed files with 85 additions and 16 deletions

13
wndclient/messages.go Normal file
View File

@@ -0,0 +1,13 @@
package wndclient
type WndRegisterTokenRequest struct {
ClientAuthHash string `json:"client_auth_hash"`
Time int64 `json:"time,string"`
}
type WndRegisterTokenResponse struct {
TokenID string `json:"id"`
Token string `json:"token"`
TokenSignature string `json:"signature"`
TokenTime int64 `json:"time,string"`
}

View File

@@ -1,9 +1,10 @@
package wndclient
import (
//"context"
//"encoding/json"
//"fmt"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
@@ -57,19 +58,19 @@ var DefaultWndSettings = WndSettings{
}
type WndClient struct {
httpClient *http.Client
Settings WndSettings
TokenID string
Token string
Signature string
SignatureTime int64
LocationHash string
LocationRevision int
IsPremium bool
Status int
UserID string
SessionAuthHash string
Mux sync.Mutex
httpClient *http.Client
Settings WndSettings
TokenID string
Token string
TokenSignature string
TokenSignatureTime int64
LocationHash string
LocationRevision int
IsPremium bool
Status int
UserID string
SessionAuthHash string
Mux sync.Mutex
}
type StrKV map[string]string
@@ -103,6 +104,61 @@ func (c *WndClient) resetCookies() error {
return (c.httpClient.Jar.(*StdJar)).Reset()
}
func (c *WndClient) RegisterToken(ctx context.Context) error {
c.Mux.Lock()
defer c.Mux.Unlock()
return nil
}
func (c *WndClient) postJSON(ctx context.Context, endpoint string, input, output interface{}) error {
var reqBuf bytes.Buffer
reqEncoder := json.NewEncoder(&reqBuf)
err := reqEncoder.Encode(input)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(
ctx,
"POST",
endpoint,
&reqBuf,
)
if err != nil {
return err
}
c.populateRequest(req)
req.Header.Set("Content-Type", "application/json")
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, headers: %#v", resp.Status, resp.Header)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(output)
cleanupBody(resp.Body)
if err != nil {
return err
}
return nil
}
func (c *WndClient) populateRequest(req *http.Request) {
req.Header.Set("User-Agent", c.Settings.UserAgent)
req.Header.Set("Origin", c.Settings.Origin)
queryValues := req.URL.Query()
queryValues.Set("platform", c.Settings.Platform)
req.URL.RawQuery = queryValues.Encode()
}
// Does cleanup of HTTP response in order to make it reusable by keep-alive
// logic of HTTP client
func cleanupBody(body io.ReadCloser) {