/
grammarbot.go
157 lines (120 loc) · 2.93 KB
/
grammarbot.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package grammarbot
import (
"encoding/json"
"errors"
"net/http"
"net/url"
"sync"
"unsafe"
)
const MaxLength = 50000
func New(apiKey string) *GrammarBot {
if apiKey == "" {
apiKey = "xyz"
}
return &GrammarBot{
Language: "en-US",
ApiKey: apiKey,
BaseURI: "http://api.grammarbot.io",
Version: "v2",
ApiName: "check",
Client: http.DefaultClient,
}
}
func Validate(text string) bool {
if ValidateErr(text) != nil {
return true
}
return false
}
var ErrExceedLimits = errors.New("Text longer than 50,000 characters is not allowed.")
var ErrEmpty = errors.New("Text is empty.")
func ValidateErr(text string) error {
if len(text) > MaxLength {
return ErrExceedLimits
}
if len(text) == 0 {
return ErrEmpty
}
return nil
}
var bufferPool = &sync.Pool{}
func acquireBuffer(size int) []byte {
result, ok := bufferPool.Get().([]byte)
if !ok || cap(result) < size {
return make([]byte, size)
}
result = result[:size]
return result
}
// Check - Check a given piece of text for grammatical errors.
func (api GrammarBot) Check(text string) (*Response, error) {
/*buf := make([]byte,
len(text) +
len(api.BaseURI) +
len(api.ApiName) +
len(api.Version) +
len(api.ApiKey) +
len(api.Language) +
len("//api_key=&text=&language=")) */
if err := ValidateErr(text); err != nil {
return nil, err
}
text = url.QueryEscape(text)
buf := acquireBuffer(
len(text) +
len(api.BaseURI) +
len(api.ApiName) +
len(api.Version) +
len(api.ApiKey) +
len(api.Language) +
len("//api_key=&text=&language="))
p := copy(buf[:], api.BaseURI)
p += copy(buf[p:], "/")
p += copy(buf[p:], api.Version)
p += copy(buf[p:], "/")
p += copy(buf[p:], api.ApiName)
endpoint := p
p += copy(buf[p:], "api_key=")
p += copy(buf[p:], api.ApiKey)
p += copy(buf[p:], "&text=")
p += copy(buf[p:], text)
p += copy(buf[p:], "&language=")
copy(buf[p:], api.Language)
req, err := http.NewRequest("POST", b2s(buf[:endpoint]), nil)
if err != nil {
return nil, err
}
req.URL.RawQuery = b2s(buf[endpoint:])
resp, err := api.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("GrammarBot.Check->response: " + resp.Status)
}
var result Response
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
bufferPool.Put(buf)
return &result, nil
}
// CheckBytes - Check a given piece of text for grammatical errors.
func (api GrammarBot) CheckBytes(text []byte) (*Response, error) {
return api.Check(b2s(text))
}
var defaultGrammarBot = *New("")
// Check - Check a given piece of text for grammatical errors.
func Check(text string) (*Response, error) {
return defaultGrammarBot.Check(text)
}
// CheckBytes - Check a given piece of text for grammatical errors.
func CheckBytes(text []byte) (*Response, error) {
return defaultGrammarBot.CheckBytes(text)
}
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}