-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
82 lines (66 loc) · 1.56 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package linkedin
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
const apiURL = "https://api.linkedin.com/v1"
// Config struct
type Config struct {
AccessToken string
}
// Client struct
type Client struct {
conf *Config
}
// Init - initialize the client
func Init(conf *Config) (*Client, error) {
if conf.AccessToken == "" {
return nil, fmt.Errorf("%v", "Access token is required")
}
return &Client{conf}, nil
}
func (li *Client) call(verb string, endpoint string, id string, path string, options map[string]interface{}) (r map[string]interface{}, e error) {
if endpoint == "people" && id == "" {
id = "~"
} else if id == "" {
e = errors.New("Id must be present")
}
var fields string
if v, ok := options["fields"]; ok {
fields = ":(" + strings.Join(v.([]string), ",") + ")"
delete(options, "fields")
}
url := fmt.Sprintf(apiURL + "/" + endpoint + "/" + id + fields + path)
body, err := json.Marshal(options)
if err != nil {
return nil, err
}
bodyStr := []byte(body)
req, err := http.NewRequest(verb, url, bytes.NewBuffer(bodyStr))
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+li.conf.AccessToken)
req.Header.Add("x-li-format", "json")
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(data, &r)
if err != nil {
return nil, err
}
if _, error := r["errorCode"]; error {
err = errors.New(string(data))
return nil, err
}
return r, e
}