This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
91 lines (75 loc) · 1.53 KB
/
util.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
83
84
85
86
87
88
89
90
91
package gphoto
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"os"
"strings"
"time"
)
//NewJSONBody create a new json request body from an interface
func NewJSONBody(model interface{}) io.Reader {
var buf = &bytes.Buffer{}
json.NewEncoder(buf).Encode(model)
return buf
}
func BodyToString(body io.Reader) string {
var buf bytes.Buffer
io.Copy(&buf, body)
return buf.String()
}
func BodyToBytes(body io.Reader) []byte {
var buf bytes.Buffer
io.Copy(&buf, body)
return buf.Bytes()
}
func NewJSONString(model interface{}) string {
var buf = &bytes.Buffer{}
json.NewEncoder(buf).Encode(model)
return buf.String()
}
func DumpRequest(request *http.Request) {
d, _ := httputil.DumpRequest(request, true)
fmt.Printf("Request: %s\n", d)
}
func DumpResponse(response *http.Response) {
d, _ := httputil.DumpResponse(response, true)
fmt.Printf("Response: %s\n", d)
}
func UnixMiliSeconds() int64 {
return time.Now().UnixNano() / 1000000
}
func SpritMagicToken(t string) []string {
return strings.Split(t, ":")
}
func JsonBodyByScanLine(s string, start, end int) string {
scanner := bufio.NewScanner(strings.NewReader(s))
i := 1
var b string
for scanner.Scan() {
text := scanner.Text()
text = strings.TrimSpace(text)
if i >= start {
b += text
if end == -1 && text == "]" {
break
}
if i == end {
break
}
}
i++
}
return b
}
func WriteStringToFile(s string) {
file, err := os.Create("file.txt")
if err != nil {
panic(err)
}
io.Copy(file, strings.NewReader(s))
}