Commit 8c6dd0499554f1acdb7b2f389880291dac4dee9d

Authored by Marko Tikvić
1 parent d76a611ee8
Exists in master

GetContent

Showing 2 changed files with 55 additions and 6 deletions   Show diff stats
1 package webutility 1 package webutility
2 2
3 import ( 3 import (
4 "bytes" 4 "bytes"
5 "io" 5 "io"
6 "io/ioutil" 6 "io/ioutil"
7 "os" 7 "os"
8 "strings" 8 "strings"
9 ) 9 )
10 10
11 // ReadFileContent ... 11 // ReadFileContent ...
12 func ReadFileContent(path string) ([]byte, error) { 12 func ReadFileContent(path string) ([]byte, error) {
13 f, err := os.Open(path) 13 f, err := os.Open(path)
14 if err != nil { 14 if err != nil {
15 return nil, err 15 return nil, err
16 } 16 }
17 defer f.Close() 17 defer f.Close()
18 18
19 buf := &bytes.Buffer{} 19 buf := &bytes.Buffer{}
20 if _, err = io.Copy(buf, f); err != nil { 20 if _, err = io.Copy(buf, f); err != nil {
21 return nil, err 21 return nil, err
22 } 22 }
23 23
24 return buf.Bytes(), nil 24 return buf.Bytes(), nil
25 } 25 }
26 26
27 // ReadFileLines ... 27 // ReadFileLines ...
28 func ReadFileLines(path string) ([]string, error) { 28 func ReadFileLines(path string) ([]string, error) {
29 f, err := os.Open(path) 29 f, err := os.Open(path)
30 if err != nil { 30 if err != nil {
31 return nil, err 31 return nil, err
32 } 32 }
33 defer f.Close() 33 defer f.Close()
34 34
35 var s strings.Builder 35 var s strings.Builder
36 36
37 if _, err = io.Copy(&s, f); err != nil { 37 if _, err = io.Copy(&s, f); err != nil {
38 return nil, err 38 return nil, err
39 } 39 }
40 40
41 lines := strings.Split(s.String(), "\n") 41 lines := strings.Split(s.String(), "\n")
42 for i := range lines { 42 for i := range lines {
43 lines[i] = strings.TrimRight(lines[i], "\r\n") 43 lines[i] = strings.TrimRight(lines[i], "\r\n")
44 } 44 }
45 45
46 return lines, nil 46 return lines, nil
47 } 47 }
48 48
49 // LinesToFile ... 49 // LinesToFile ...
50 func LinesToFile(path string, lines []string) error { 50 func LinesToFile(path string, lines []string) error {
51 content := "" 51 content := ""
52 for _, l := range lines { 52 for _, l := range lines {
53 content += l + "\n" 53 content += l + "\n"
54 } 54 }
55 55
56 return ioutil.WriteFile(path, []byte(content), 0644) // drw-r--r-- 56 return ioutil.WriteFile(path, []byte(content), 0644) // drw-r--r--
57 } 57 }
58 58
59 // InsertLine ... 59 // InsertLine ...
60 func InsertLine(lines *[]string, pos int64, l string) { 60 func InsertLine(lines *[]string, pos int64, l string) {
61 tail := append([]string{l}, (*lines)[pos:]...) 61 tail := append([]string{l}, (*lines)[pos:]...)
62 62
63 *lines = append((*lines)[:pos], tail...) 63 *lines = append((*lines)[:pos], tail...)
64 } 64 }
65 65
66 func WriteFile(path string, content []byte) error {
67 return ioutil.WriteFile(path, content, 0644) // drw-r--r--
68 }
66 69
1 package webutility 1 package webutility
2 2
3 // TODO(marko): If DecodeJSON() returns io.EOF treat it as if there is no response body, since response content length can sometimes be -1. 3 // TODO(marko): If DecodeJSON() returns io.EOF treat it as if there is no response body, since response content length can sometimes be -1.
4 4
5 import ( 5 import (
6 "bytes" 6 "bytes"
7 "encoding/json" 7 "encoding/json"
8 "io" 8 "io"
9 "io/ioutil"
9 "net/http" 10 "net/http"
10 "net/url" 11 "net/url"
11 ) 12 )
12 13
14 func GetContent(url string, params url.Values, headers http.Header) (content []byte, status int, err error) {
15 if params != nil {
16 p := params.Encode()
17 if p != "" {
18 url += "?" + p
19 }
20 }
21
22 req, err := http.NewRequest(http.MethodGet, url, nil)
23 if err != nil {
24 return nil, 0, err
25 }
26
27 if headers != nil {
28 for k, head := range headers {
29 for i, h := range head {
30 if i == 0 {
31 req.Header.Set(k, h)
32 } else {
33 req.Header.Add(k, h)
34 }
35 }
36 }
37 }
38
39 resp, err := http.DefaultClient.Do(req)
40 if err != nil {
41 return nil, 0, err
42 }
43 defer resp.Body.Close()
44 status = resp.StatusCode
45
46 if status != http.StatusOK {
47 return nil, status, err
48 }
49
50 content, err = ioutil.ReadAll(resp.Body)
51
52 return content, status, err
53 }
54
13 // DecodeJSON decodes JSON data from r to v. 55 // DecodeJSON decodes JSON data from r to v.
14 // Returns an error if it fails. 56 // Returns an error if it fails.
15 func DecodeJSON(r io.Reader, v interface{}) error { 57 func DecodeJSON(r io.Reader, v interface{}) error {
16 return json.NewDecoder(r).Decode(v) 58 return json.NewDecoder(r).Decode(v)
17 } 59 }
18 60
19 func GetJSON(url string, v interface{}, params url.Values, headers http.Header) (status int, err error) { 61 func GetJSON(url string, v interface{}, params url.Values, headers http.Header) (status int, err error) {
20 p := params.Encode() 62 if params != nil {
21 if p != "" { 63 p := params.Encode()
22 url += "?" + p 64 if p != "" {
65 url += "?" + p
66 }
23 } 67 }
24 68
25 req, err := http.NewRequest(http.MethodGet, url, nil) 69 req, err := http.NewRequest(http.MethodGet, url, nil)
26 if err != nil { 70 if err != nil {
27 return 0, err 71 return 0, err
28 } 72 }
29 73
30 if headers != nil { 74 if headers != nil {
31 for k, head := range headers { 75 for k, head := range headers {
32 for i, h := range head { 76 for i, h := range head {
33 if i == 0 { 77 if i == 0 {
34 req.Header.Set(k, h) 78 req.Header.Set(k, h)
35 } else { 79 } else {
36 req.Header.Add(k, h) 80 req.Header.Add(k, h)
37 } 81 }
38 } 82 }
39 } 83 }
40 } 84 }
41 85
42 resp, err := http.DefaultClient.Do(req) 86 resp, err := http.DefaultClient.Do(req)
43 if err != nil { 87 if err != nil {
44 return 0, err 88 return 0, err
45 } 89 }
46 defer resp.Body.Close() 90 defer resp.Body.Close()
47 status = resp.StatusCode 91 status = resp.StatusCode
48 92
49 if status != http.StatusOK { 93 if status != http.StatusOK {
50 return status, err 94 return status, err
51 } 95 }
52 96
53 if err = DecodeJSON(resp.Body, v); err == io.EOF { 97 if err = DecodeJSON(resp.Body, v); err == io.EOF {
54 err = nil 98 err = nil
55 } 99 }
56 100
57 return status, err 101 return status, err
58 } 102 }
59 103
60 func PostJSON(url string, v, r interface{}, params url.Values, headers http.Header) (status int, err error) { 104 func PostJSON(url string, v, r interface{}, params url.Values, headers http.Header) (status int, err error) {
61 buffer := bytes.NewBuffer(make([]byte, 0)) 105 buffer := bytes.NewBuffer(make([]byte, 0))
62 json.NewEncoder(buffer).Encode(v) 106 json.NewEncoder(buffer).Encode(v)
63 107
64 p := params.Encode() 108 if params != nil {
65 if p != "" { 109 p := params.Encode()
66 url += "?" + p 110 if p != "" {
111 url += "?" + p
112 }
67 } 113 }
68 114
69 req, err := http.NewRequest(http.MethodPost, url, buffer) 115 req, err := http.NewRequest(http.MethodPost, url, buffer)
70 if err != nil { 116 if err != nil {
71 return 0, err 117 return 0, err
72 } 118 }
73 119
74 if headers != nil { 120 if headers != nil {
75 for k, head := range headers { 121 for k, head := range headers {
76 for i, h := range head { 122 for i, h := range head {
77 if i == 0 { 123 if i == 0 {
78 req.Header.Set(k, h) 124 req.Header.Set(k, h)
79 } else { 125 } else {
80 req.Header.Add(k, h) 126 req.Header.Add(k, h)
81 } 127 }
82 } 128 }
83 } 129 }
84 } 130 }
85 req.Header.Set("Content-Type", "application/json") 131 req.Header.Set("Content-Type", "application/json")
86 132
87 resp, err := http.DefaultClient.Do(req) 133 resp, err := http.DefaultClient.Do(req)
88 if err != nil { 134 if err != nil {
89 return 0, err 135 return 0, err
90 } 136 }
91 status = resp.StatusCode 137 status = resp.StatusCode
92 defer resp.Body.Close() 138 defer resp.Body.Close()
93 139
94 if status != http.StatusOK && status != http.StatusCreated { 140 if status != http.StatusOK && status != http.StatusCreated {
95 return status, err 141 return status, err
96 } 142 }
97 143
98 if err = DecodeJSON(resp.Body, v); err == io.EOF { 144 if err = DecodeJSON(resp.Body, v); err == io.EOF {
99 err = nil 145 err = nil
100 } 146 }
101 147
102 return status, err 148 return status, err
103 } 149 }
104 150