forked from space307/go-lmax-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
46 lines (40 loc) · 898 Bytes
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package lmax
import (
"bytes"
"io"
"net/http"
"github.com/space307/go-lmax-api/account"
"github.com/space307/go-lmax-api/core"
"github.com/space307/go-lmax-api/model"
)
type (
// API ...
API struct {
httpInvoker *core.HttpInvoker
}
)
// NewAPI ...
func NewAPI(addr string) *API {
return &API{
httpInvoker: core.NewHttpInvoker(addr),
}
}
func (api *API) Login(msg *account.LoginRequest, callback account.LoginResponse) error {
args := bytes.NewBuffer(nil)
err := msg.Write(args)
if err != nil {
return err
}
return api.httpInvoker.Post(msg.RequestURI(), msg.Header(), args, func(code int, header model.Header, reader io.Reader) error {
if code != http.StatusOK {
callback.OnFailure(code, reader)
return nil
}
session, err := core.NewSession(header, reader, api.httpInvoker)
if err != nil {
return err
}
callback.OnSuccess(session)
return nil
})
}