-
Notifications
You must be signed in to change notification settings - Fork 1
/
languagetool.go
49 lines (42 loc) · 1.12 KB
/
languagetool.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
package languagetool
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
// CheckWithURL runs an api call for a custom languagetool server
func CheckWithURL(text, lang, apiURL string) (Result, error) {
data := url.Values{}
data.Set("text", text)
data.Set("language", lang)
data.Set("enabledOnly", "false")
req, err := http.NewRequest("POST", apiURL, bytes.NewBufferString(data.Encode()))
if err != nil {
return Result{}, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return Result{}, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
result := Result{}
err = json.Unmarshal(body, &result)
if err != nil {
return Result{}, err
}
if resp.StatusCode == 200 {
return result, nil
}
// TODO handle other status codes
return Result{}, nil
}
// Check runs an api call to the public LanguageTool API, to check the given text with the given lang
func Check(text, lang string) (Result, error) {
return CheckWithURL(text, lang, URL)
}