package main
import (
"encoding/json"
"fmt"
"net/http"
"io/ioutil"
"bytes"
"strings"
)
type TranslateJson struct {
FieldId string `json:"fieldId"`
Text []string `json:"text"`
TargetLang string `json:"targetLang"`
SourceLang string `json:"sourceLang"`
ContractId string `json:"contractId"`
}
// Post text and get translation results
func main() {
url := "https://translate.rozetta-api.io/translate/api/v1/token"
method := "POST"
payload := strings.NewReader(`{
"accessKey": "YOUR_ACCESS_KEY",
"secretKey": "YOUR_SECRET_KEY",
"duration": 10000
}`)
tokenClient := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
res, err := tokenClient.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
var jsondata map[string]interface{}
json.Unmarshal([]byte(body), &jsondata)
token := jsondata["data"].(map[string]interface{})["encodedJWT"].(string)
testJson := &TranslateJson{
FieldId: "1",
Text: []string{"This is a pen.", "I have an apple."},
TargetLang: "ja",
SourceLang: "en",
ContractId: "YOUR_CONTRACT_ID"}
jsonValue, _ := json.Marshal(testJson)
client := &http.Client{}
translateReq, err := http.NewRequest(
"POST",
"https://translate.rozetta-api.io/translate/api/v1/translate",
bytes.NewBuffer([]byte(jsonValue)),
)
translateReq.Header.Set("Content-Type", "application/json")
translateReq.Header.Set("Authorization", "Bearer " + token)
resp, err := client.Do(translateReq)
if err != nil {
fmt.Printf("The HTTP request failed with error %s
", err)
} else {
data, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(data))
}
}