Skip to content

Commit

Permalink
refactor: Breaking changes to translate options implementation and ap…
Browse files Browse the repository at this point in the history
…i call methods
  • Loading branch information
cluttrdev committed Nov 15, 2023
1 parent f87e419 commit 93b930b
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 130 deletions.
35 changes: 19 additions & 16 deletions api/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"path/filepath"
"strings"
)

type DocumentInfo struct {
Expand All @@ -27,7 +28,7 @@ type DocumentStatus struct {
Message string `json:"message"`
}

func (t *Translator) TranslateDocumentUpload(filePath string, targetLang string, options ...TranslateOption) (*DocumentInfo, error) {
func (t *Translator) TranslateDocumentUpload(filePath string, targetLang string, opts ...TranslateOptionFunc) (*DocumentInfo, error) {
var (
err error
f *os.File
Expand All @@ -41,6 +42,11 @@ func (t *Translator) TranslateDocumentUpload(filePath string, targetLang string,
log.Fatal(err)
}

options := TranslateOptions{}
if err := options.Gather(opts...); err != nil {
return nil, fmt.Errorf("error gathering options: %w", err)
}

r, w := io.Pipe()
mpw := multipart.NewWriter(w)
go func() {
Expand All @@ -52,10 +58,10 @@ func (t *Translator) TranslateDocumentUpload(filePath string, targetLang string,
mpw.WriteField("filename", filepath.Base(filePath))
mpw.WriteField("target_lang", targetLang)

for _, opt := range options {
switch opt.Key {
for name, value := range options {
switch name {
case "source_lang", "formality", "glossary_id":
mpw.WriteField(opt.Key, opt.Value)
mpw.WriteField(name, value)
}
}

Expand All @@ -71,17 +77,10 @@ func (t *Translator) TranslateDocumentUpload(filePath string, targetLang string,
}
}()

url := fmt.Sprintf("%s/%s", t.serverURL, "document")
headers := make(http.Header)
headers.Set("Content-Type", mpw.FormDataContentType())

req, err := http.NewRequest("POST", url, r)
if err != nil {
log.Fatal(err)
}

req.Header.Set("Authorization", fmt.Sprintf("DeepL-Auth-Key %s", t.authKey))
req.Header.Add("Content-Type", mpw.FormDataContentType())

res, err := t.client.Do(req)
res, err := t.callAPI(http.MethodPost, "document", headers, r)
if err != nil {
return nil, err
} else if res.StatusCode != http.StatusOK {
Expand All @@ -103,7 +102,9 @@ func (t *Translator) TranslateDocumentStatus(id string, key string) (*DocumentSt
vals := make(url.Values)
vals.Set("document_key", key)

res, err := t.callAPI("POST", endpoint, vals, nil)
body := strings.NewReader(vals.Encode())

res, err := t.callAPI(http.MethodPost, endpoint, nil, body)
if err != nil {
return nil, err
} else if res.StatusCode != http.StatusOK {
Expand All @@ -125,7 +126,9 @@ func (t *Translator) TranslateDocumentDownload(id string, key string) (*io.PipeR
vals := make(url.Values)
vals.Set("document_key", key)

res, err := t.callAPI("POST", endpoint, vals, nil)
body := strings.NewReader(vals.Encode())

res, err := t.callAPI(http.MethodPost, endpoint, nil, body)
if err != nil {
return nil, err
} else if res.StatusCode != http.StatusOK {
Expand Down
15 changes: 10 additions & 5 deletions api/glossaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ func (t *Translator) CreateGlossary(name string, sourceLang string, targetLang s
}
vals.Set("entries", strings.Join(entriesTSV, "\n"))

res, err := t.callAPI("POST", "glossaries", vals, nil)
headers := make(http.Header)
headers.Set("Content-Type", "application/x-www-form-urlencoded")

body := strings.NewReader(vals.Encode())

res, err := t.callAPI(http.MethodPost, "glossaries", headers, body)
if err != nil {
return nil, err
} else if res.StatusCode != http.StatusCreated {
Expand All @@ -55,7 +60,7 @@ func (t *Translator) CreateGlossary(name string, sourceLang string, targetLang s
}

func (t *Translator) ListGlossaries() ([]GlossaryInfo, error) {
res, err := t.callAPI("GET", "glossaries", nil, nil)
res, err := t.callAPI(http.MethodGet, "glossaries", nil, nil)
if err != nil {
return nil, err
} else if res.StatusCode != http.StatusOK {
Expand All @@ -76,7 +81,7 @@ func (t *Translator) ListGlossaries() ([]GlossaryInfo, error) {
func (t *Translator) GetGlossary(glossaryId string) (*GlossaryInfo, error) {
endpoint := fmt.Sprintf("glossaries/%s", glossaryId)

res, err := t.callAPI("GET", endpoint, nil, nil)
res, err := t.callAPI(http.MethodGet, endpoint, nil, nil)
if err != nil {
return nil, err
} else if res.StatusCode != http.StatusOK {
Expand All @@ -95,7 +100,7 @@ func (t *Translator) GetGlossary(glossaryId string) (*GlossaryInfo, error) {
func (t *Translator) DeleteGlossary(glossaryId string) error {
endpoint := fmt.Sprintf("glossaries/%s", glossaryId)

res, err := t.callAPI("DELETE", endpoint, nil, nil)
res, err := t.callAPI(http.MethodDelete, endpoint, nil, nil)
if err != nil {
return err
} else if res.StatusCode != http.StatusNoContent {
Expand All @@ -112,7 +117,7 @@ func (t *Translator) GetGlossaryEntries(glossaryId string) ([]GlossaryEntry, err
headers := http.Header{}
headers.Set("Accept", "text/tab-separated-values")

res, err := t.callAPI("GET", endpoint, nil, headers)
res, err := t.callAPI(http.MethodGet, endpoint, headers, nil)
if err != nil {
return nil, err
} else if res.StatusCode != http.StatusOK {
Expand Down
8 changes: 6 additions & 2 deletions api/languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"net/url"
"strings"
)

type Language struct {
Expand All @@ -24,7 +25,10 @@ func (t *Translator) GetLanguages(langType string) ([]Language, error) {
vals.Set("type", langType)
}

res, err := t.callAPI("GET", "languages", vals, nil)
data := vals.Encode()
body := strings.NewReader(data)

res, err := t.callAPI(http.MethodGet, "languages", nil, body)
if err != nil {
return nil, err
} else if res.StatusCode != http.StatusOK {
Expand All @@ -41,7 +45,7 @@ func (t *Translator) GetLanguages(langType string) ([]Language, error) {
}

func (t *Translator) GetGlossaryLanguagePairs() ([]LanguagePair, error) {
res, err := t.callAPI("GET", "glossary-language-pairs", nil, nil)
res, err := t.callAPI(http.MethodGet, "glossary-language-pairs", nil, nil)
if err != nil {
return nil, err
} else if res.StatusCode != http.StatusOK {
Expand Down
Loading

0 comments on commit 93b930b

Please sign in to comment.