-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
56 lines (46 loc) · 1.21 KB
/
client.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
49
50
51
52
53
54
55
56
package avatar
import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
// Client initializes a new avatar api client
type Client struct {
Username string
Password string
}
// Profile is the structure returned in GetResponse()
type Profile struct {
Name string `xml:"Name"`
Image string `xml:"Image"`
Valid string `xml:"Valid"`
}
// GetResponse takes in an email and returns
// an avatar api profile structure.
func (c *Client) GetResponse(email string) (*Profile, error) {
if c.Username == "" || c.Password == "" {
return nil, errors.New("Must have valid username and password")
}
res := Profile{}
// "http://www.avatarapi.com/avatar.asmx/GetProfile?email=peter.smith@gmail.com&username=xxxxx&password=xxxxx"
url := fmt.Sprintf("http://www.avatarapi.com/avatar.asmx/GetProfile?email="+email+"&username=%+v&password=%+v", c.Username, c.Password)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Status Code was: %+v", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = xml.Unmarshal(body, &res)
if err != nil {
return nil, err
}
return &res, nil
}