-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
75 lines (64 loc) · 1.76 KB
/
utils.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
package gotwitter
import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"strings"
)
// GetConfig retrive configurations from keys.conf file.
// The format of keys.conf file is like:
// consumerKey=<your consumer key>
// consumerSecret=<your consumer screte>
// name=<your application name> /*optional*/
// token=<your authorized token> /*optional*/
func GetConfig(debug int, filename string) (map[string]string, error) {
configs := make(map[string]string)
// Read content of .conf file
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
br := bufio.NewReader(file)
for {
line, err := br.ReadString('\n')
line = strings.Trim(line, " \n") // remove '\n'
if debug > 1 {
fmt.Printf("[DEBUG 2]getConfig(): read line from .conf flie <---> %s\n", line)
}
if strings.Contains(line, "=") {
kv := strings.Split(line, "=")
configs[kv[0]] = kv[1]
}
if err != nil {
break
}
}
return configs, nil
}
func addHeadersForAuthRequest(req *http.Request, appName, credentials string) {
addHeadersForAllRequest(req, appName)
req.Header.Add("Authorization", "Basic "+credentials)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
}
func addHeadersForRetrieveRequest(req *http.Request, appName, token string) {
addHeadersForAllRequest(req, appName)
req.Header.Add("Authorization", "Bearer "+token)
}
func addHeadersForAllRequest(req *http.Request, appName string) {
req.Header.Add("User-Agent", appName)
req.Header.Add("Accept-Encoding", "gzip")
}
type debugReader struct {
r io.Reader
}
func (drc debugReader) Read(buff []byte) (int, error) {
n, err := drc.r.Read(buff)
if err != nil {
return n, err
}
fmt.Printf("[DEBUG 2] Buffer[0:%d] <---> %s\n", n, string(buff[:n]))
return n, nil
}