-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
118 additions
and
101 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package deepl | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"path" | ||
) | ||
|
||
type Client struct { | ||
BaseURL *url.URL | ||
HTTPClient *http.Client | ||
AuthKey string | ||
} | ||
|
||
func NewClient(rawBaseURL, authKey string) (*Client, error) { | ||
if len(authKey) == 0 { | ||
return nil, errors.New("missing DeepL authkey") | ||
} | ||
|
||
baseURL, err := url.ParseRequestURI(rawBaseURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Client{ | ||
BaseURL: baseURL, | ||
HTTPClient: http.DefaultClient, | ||
AuthKey: authKey, | ||
}, nil | ||
} | ||
|
||
func (c *Client) newRequest(ctx context.Context, method, _path string, body io.Reader) (*http.Request, error) { | ||
u := *c.BaseURL | ||
u.Path = path.Join(c.BaseURL.Path, _path) | ||
|
||
req, err := http.NewRequest(method, u.String(), body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req = req.WithContext(ctx) | ||
req.Header.Set("Authorization", fmt.Sprintf("DeepL-Auth-Key %s", c.AuthKey)) | ||
|
||
return req, nil | ||
} | ||
|
||
func decodeBody(resp *http.Response, out interface{}) error { | ||
success := resp.StatusCode >= 200 && resp.StatusCode < 300 | ||
if !success { | ||
return HandleHTTPError(resp) | ||
} | ||
|
||
if resp.StatusCode == http.StatusNoContent { | ||
return nil | ||
} | ||
|
||
decoder := json.NewDecoder(resp.Body) | ||
return decoder.Decode(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package api | ||
package deepl | ||
|
||
import ( | ||
"encoding/json" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package deepl | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
const TRANSLATE = "/translate" | ||
|
||
type TranslateResponse struct { | ||
Translations []struct { | ||
DetectedSourceLanguage string `json:"detected_source_language"` | ||
Text string `json:"text"` | ||
} `json:"translations"` | ||
} | ||
|
||
func (c *Client) Translate(ctx context.Context, text, sourceLang, targetLang string) (string, error) { | ||
values := url.Values{ | ||
"text": {text}, | ||
"source_lang": {sourceLang}, | ||
"target_lang": {targetLang}, | ||
} | ||
req, err := c.newRequest(ctx, "POST", TRANSLATE, strings.NewReader(values.Encode())) | ||
if err != nil { | ||
return "", err | ||
} | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
||
resp, err := c.HTTPClient.Do(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
transalted := new(TranslateResponse) | ||
if err := decodeBody(resp, transalted); err != nil { | ||
return "", err | ||
} | ||
|
||
return transalted.Translations[0].Text, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters