-
Notifications
You must be signed in to change notification settings - Fork 0
/
zoomeye.go
48 lines (45 loc) · 954 Bytes
/
zoomeye.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
47
48
package zoomeye // github.com/Earth-Online/zoomeye-api
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
// Zoomeye login get token
func UserLogin(username string, password string) (Token, error) {
var login = Login{
Username: username,
Password: password,
}
var jsonStr, _ = json.Marshal(login)
resp, err := http.Post(LoginApi, "application/json", bytes.NewBuffer(jsonStr))
if err != nil {
return Token{}, err
}
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return Token{}, errors.New(string(body))
}
token := Token{}
err = json.Unmarshal(body, &token)
if err != nil {
return Token{}, errors.New(string(body))
}
return token, nil
}
// Get new user
func NewUser(username string, password string) (User, error) {
token, err := UserLogin(username, password)
if err != nil {
return User{}, err
}
user := User{
Login{
username,
password,
},
token,
}
return user, nil
}