forked from Smartling/api-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
127 lines (107 loc) · 4.56 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2020 Smartling
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Package smartling is a client implementation of the Smartling Translation
// API v2 as documented at https://help.smartling.com/v1.0/reference
package smartling
import (
"encoding/json"
"io"
"net/http"
"net/url"
"time"
)
type ClientInterface interface {
Authenticate() error
DeleteFile(projectID string, uri string) error
DownloadFile(projectID string, uri string) (io.Reader, error)
DownloadTranslation(projectID string, localeID string, request FileDownloadRequest) (io.Reader, error)
GetFileStatus(projectID string, fileURI string) (*FileStatus, error)
Get(url string, params url.Values, options ...interface{}) (io.ReadCloser, int, error)
GetJSON(url string, params url.Values, result interface{}, options ...interface{}) (json.RawMessage, int, error)
GetProjectDetails(projectID string) (*ProjectDetails, error)
Import(projectID string, localeID string, request ImportRequest) (*FileImportResult, error)
LastModified(projectID string, request FileLastModifiedRequest) (*FileLastModifiedLocales, error)
ListAllFiles(projectID string, request FilesListRequest) ([]File, error)
ListFiles(projectID string, request FilesListRequest) (*FilesList, error)
ListFileTypes(projectID string) ([]FileType, error)
ListProjects(accountID string, request ProjectsListRequest) (*ProjectsList, error)
Post(url string, payload []byte, result interface{}, options ...interface{}) (json.RawMessage, int, error)
RenameFile(projectID string, oldURI string, newURI string) error
UploadFile(projectID string, request FileUploadRequest) (*FileUploadResult, error)
}
type (
// LogFunction represents abstract logger function interface which
// can be used for setting up logging of library actions.
LogFunction func(format string, args ...interface{})
)
var (
// Version is a API SDK version, sent in User-Agent header.
Version = "1.0"
// DefaultBaseURL specifies base URL which will be used for calls unless
// other is specified in the Client struct.
DefaultBaseURL = "https://api.smartling.com"
// DefaultHTTPClient specifies default HTTP client which will be used
// for calls unless other is specified in the Client struct.
DefaultHTTPClient = http.Client{Timeout: 120 * time.Second}
// DefaultUserAgent is a string that will be sent in User-Agent header.
DefaultUserAgent = "smartling-api-sdk-go"
)
// Client represents Smartling API client.
type Client struct {
BaseURL string
Credentials *Credentials
HTTP *http.Client
Logger struct {
Infof LogFunction
Debugf LogFunction
}
UserAgent string
}
// NewClient returns new Smartling API client with specified authentication
// data.
func NewClient(userID string, tokenSecret string) *Client {
return &Client{
BaseURL: DefaultBaseURL,
Credentials: &Credentials{
UserID: userID,
Secret: tokenSecret,
},
HTTP: &DefaultHTTPClient,
Logger: struct {
Infof LogFunction
Debugf LogFunction
}{
Infof: func(string, ...interface{}) {},
Debugf: func(string, ...interface{}) {},
},
UserAgent: DefaultUserAgent + "/" + Version,
}
}
// SetInfoLogger sets logger function which will be called for logging
// informational messages like progress of file download and so on.
func (client *Client) SetInfoLogger(logger LogFunction) *Client {
client.Logger.Infof = logger
return client
}
// SetDebugLogger sets logger function which will be called for logging
// internal information like HTTP requests and their responses.
func (client *Client) SetDebugLogger(logger LogFunction) *Client {
client.Logger.Debugf = logger
return client
}