Skip to content

Commit

Permalink
feat: support json or form data in v2/translate
Browse files Browse the repository at this point in the history
  • Loading branch information
missuo committed Apr 16, 2024
1 parent e15e63b commit 90d4d44
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Vincent Young
* @Date: 2022-10-18 07:32:29
* @LastEditors: Vincent Young
* @LastEditTime: 2024-04-16 14:52:46
* @LastEditTime: 2024-04-16 15:10:38
* @FilePath: /DeepLX/README.md
* @Telegram: https://t.me/missuo
*
Expand Down Expand Up @@ -71,17 +71,18 @@
- `-authkey` : DeepL Official `AuthKey`. If you have set it up, after the 429 response, the official AuthKey will be used for the request. If multiple authKeys are used simultaneously, they need to be separated by commas.
- `/v2/translate` : This endpoint is fully compatible with the DeepL official API. When using this endpoint, please strictly adhere to the request styles outlined in the official DeepL documentation. Note that in this endpoint, please use `DeepL-Auth-Key $token` in the `Authorization`, which is actually the Access Token, not the official `Auth Key` of DeepL.

#### Example of requesting a token-protected `/v2/translate` endpoint
```bash
# Example of requesting a token-protected /v2/translate endpoint
curl -X POST 'http://127.0.0.1:1188/v2/translate' \
--header 'Authorization: DeepL-Auth-Key [yourAccessToken]' \
curl -X POST 'http://localhost:1188/v2/translate' \
--header 'Authorization: DeepL-Auth-Key [yourAccessToken] [yourAuthKey]' \
--header 'Content-Type: application/json' \
--data '{
"text": [
"Hello, world!"
],
"target_lang": "DE"
}'
# Please note that either `yourAccessToken` or `yourAuthKey` can be omitted.
```

#### Requesting a token-protected **DeepLX API** instance using the `curl`
Expand Down
43 changes: 35 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Vincent Yang
* @Date: 2023-07-01 21:45:34
* @LastEditors: Vincent Young
* @LastEditTime: 2024-04-16 14:47:43
* @LastEditTime: 2024-04-16 15:07:54
* @FilePath: /DeepLX/main.go
* @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo
Expand Down Expand Up @@ -434,21 +434,48 @@ func main() {

r.POST("/v2/translate", authMiddleware(cfg), func(c *gin.Context) {
authorizationHeader := c.GetHeader("Authorization")
parts := strings.Split(authorizationHeader, " ")
var authKey string
if len(parts) == 2 {
authKey = parts[1]

if strings.HasPrefix(authorizationHeader, "DeepL-Auth-Key") {
parts := strings.Split(authorizationHeader, " ")
if len(parts) >= 2 && strings.HasSuffix(parts[len(parts)-1], ":fx") {
authKey = parts[len(parts)-1]
}
}

var translateText string
var targetLang string

translateText = c.PostForm("text")
targetLang = c.PostForm("target_lang")

if translateText == "" || targetLang == "" {
var jsonData struct {
Text []string `json:"text"`
TargetLang string `json:"target_lang"`
}

if err := c.BindJSON(&jsonData); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": "Invalid request payload",
})
return
}

translateText = strings.Join(jsonData.Text, "\n")
targetLang = jsonData.TargetLang
}
translateText := c.PostForm("text")
targetLang := c.PostForm("target_lang")

result, err := translateByDeepLX("", targetLang, translateText, authKey)
if err != nil {
log.Fatalf("Translation failed: %s", err)
}

if result.Code == http.StatusOK {
c.JSON(http.StatusOK, gin.H{
"translations": []interface{}{
map[string]interface{}{
"translations": []map[string]interface{}{
{
"detected_source_language": result.SourceLang,
"text": result.Data,
},
Expand Down

0 comments on commit 90d4d44

Please sign in to comment.