Skip to content

Commit

Permalink
Refactor: http reqeust
Browse files Browse the repository at this point in the history
  • Loading branch information
candy12t committed Jan 12, 2024
1 parent 5f4a50e commit 5b8cb09
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
30 changes: 30 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,33 @@ func (c *Client) httpClient() *http.Client {
}
return http.DefaultClient
}

func (c *Client) Get(ctx context.Context, path string, query url.Values, v any) (*http.Response, error) {
req, err := c.NewRequest(ctx, http.MethodGet, path, query, nil)
if err != nil {
return nil, err
}
return c.Do(req, v)
}

func (c *Client) Post(ctx context.Context, path string, body io.Reader, v any) (*http.Response, error) {
req, err := c.NewRequest(ctx, http.MethodPost, path, nil, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
return c.Do(req, v)
}

// TODO
func (c *Client) UploadFile(ctx context.Context, path string, body io.Reader, v any) (*http.Response, error) {
return nil, nil
}

func (c *Client) Delete(ctx context.Context, path string) (*http.Response, error) {
req, err := c.NewRequest(ctx, http.MethodDelete, path, nil, nil)
if err != nil {
return nil, err
}
return c.Do(req, nil)
}
18 changes: 4 additions & 14 deletions languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package deepl

import (
"context"
"net/http"
"net/url"
)

Expand All @@ -20,16 +19,12 @@ type Language struct {
}

func (c *Client) GetLanguages(ctx context.Context, langType langType) ([]Language, error) {
var languages []Language

query := url.Values{}
query.Add("type", string(langType))

req, err := c.NewRequest(ctx, http.MethodGet, "/languages", query, nil)
if err != nil {
return nil, err
}

var languages []Language
if _, err := c.Do(req, &languages); err != nil {
if _, err := c.Get(ctx, "/languages", query, &languages); err != nil {
return nil, err
}
return languages, nil
Expand All @@ -43,13 +38,8 @@ type GlossaryLanguagePairs struct {
}

func (c *Client) GetGlossaryLanguagesPairs(ctx context.Context) (*GlossaryLanguagePairs, error) {
req, err := c.NewRequest(ctx, http.MethodGet, "/glossary-language-pairs", nil, nil)
if err != nil {
return nil, err
}

languages := new(GlossaryLanguagePairs)
if _, err := c.Do(req, languages); err != nil {
if _, err := c.Get(ctx, "/glossary-language-pairs", nil, &languages); err != nil {
return nil, err
}
return languages, nil
Expand Down
8 changes: 1 addition & 7 deletions usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package deepl

import (
"context"
"net/http"
)

type Usage struct {
Expand All @@ -15,13 +14,8 @@ type Usage struct {
}

func (c *Client) GetUsage(ctx context.Context) (*Usage, error) {
req, err := c.NewRequest(ctx, http.MethodGet, "/usage", nil, nil)
if err != nil {
return nil, err
}

usage := new(Usage)
if _, err := c.Do(req, usage); err != nil {
if _, err := c.Get(ctx, "/usage", nil, usage); err != nil {
return nil, err
}
return usage, nil
Expand Down

0 comments on commit 5b8cb09

Please sign in to comment.