-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
99 lines (81 loc) · 2.29 KB
/
client.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
const (
libraryVersion = "1.0.0"
defaultBaseURL = "https://translation.googleapis.com/"
userAgent = "translate/" + libraryVersion + " (+https://github.com/romantomjak/translate)"
)
// Translation is a translation request result
type Translation struct {
TranslatedText string `json:"translatedText"`
DetectedSourceLanguage string `json:"detectedSourceLanguage"`
}
// Client manages communication with Cloud Translation API
type Client struct {
// HTTP client used to communicate with CT API
client *http.Client
// Google Cloud Translation API key
apiKey string
// User agent for client
UserAgent string
// Base URL for API requests.
BaseURL *url.URL
}
// NewClient returns a new Cloud Translation API client
func NewClient(APIKey string) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
return &Client{
client: http.DefaultClient,
apiKey: APIKey,
UserAgent: userAgent,
BaseURL: baseURL,
}
}
// Translate method translates a string with given parameters
func (c *Client) Translate(fromLang, toLang string, text []string) ([]Translation, error) {
data := url.Values{
"key": {c.apiKey},
"q": text,
"target": {toLang},
"source": {fromLang},
"format": {"text"},
}
req, err := c.newRequest(http.MethodPost, "language/translate/v2", data)
if err != nil {
return nil, fmt.Errorf("cannot initialise request: %v", err)
}
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("cannot execute request: %v", err)
}
var translationResp struct {
Data struct {
Translations []Translation `json:"translations"`
} `json:"data"`
}
err = json.NewDecoder(resp.Body).Decode(&translationResp)
if err != nil {
return nil, fmt.Errorf("cannot decode json: %v", err)
}
return translationResp.Data.Translations, nil
}
func (c *Client) newRequest(method, path string, data url.Values) (*http.Request, error) {
rel, err := url.Parse(path)
if err != nil {
return nil, err
}
u := c.BaseURL.ResolveReference(rel)
req, err := http.NewRequest(method, u.String(), strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", c.UserAgent)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req, nil
}