/
configuration.go
154 lines (128 loc) · 4.52 KB
/
configuration.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* --------------------------------------------------------------------------------
* <copyright company="Aspose" file="configuration.go">
* Copyright (c) 2022 Aspose.Words for Cloud
* </copyright>
* <summary>
* 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.
* </summary>
* --------------------------------------------------------------------------------
*/
package models
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"path"
"time"
)
// contextKeys are used to identify the type of value in the context.
// Since these are string, it is possible to get a short description of the
// context key for logging and debugging using key.String().
type contextKey string
func (c contextKey) String() string {
return "auth " + string(c)
}
var (
// ContextOAuth2 takes a oauth2.TokenSource as authentication for the request.
ContextOAuth2 = contextKey("token")
// ContextBasicAuth takes BasicAuth as authentication for the request.
ContextBasicAuth = contextKey("basic")
// ContextAccessToken takes a string oauth2 access token as authentication for the request.
ContextAccessToken = contextKey("accesstoken")
// ContextclientSecret takes an ClientSecret as authentication for the request
ContextClientSecret = contextKey("clientsecret")
// ContextPasswordEncryptionKey takes a RSA key to encrypt passwords
ContextPasswordEncryptionKey = "rsakey"
)
// BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth
type BasicAuth struct {
UserName string `json:"userName,omitempty"`
Password string `json:"password,omitempty"`
}
// ClientSecret provides API key based authentication to a request passed via context using ContextclientSecret
type ClientSecret struct {
Key string
Prefix string
}
type Configuration struct {
ClientId string `json:"ClientId"`
ClientSecret string `json:"ClientSecret"`
BaseUrl string `json:"BaseUrl,omitempty"`
DebugMode bool `json:"DebugMode,omitempty"`
DefaultHeader map[string]string `json:"DefaultHeader,omitempty"`
HttpClient *http.Client
Timeout Duration `json:"Timeout,omitempty"`
Modulus string `json:"Modulus,omitempty"`
Exponent string `json:"Exponent,omitempty"`
}
func NewConfiguration(configFilePath string) (pConfig *Configuration, err error) {
data, err := ioutil.ReadFile(configFilePath)
if err != nil {
return nil, err
}
cfg := Configuration{
BaseUrl: "https://api.aspose.cloud",
DebugMode: false,
DefaultHeader: map[string]string{"x-aspose-client": "go sdk", "x-aspose-client-version": "22.12"},
}
err = json.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}
u, err := url.Parse(cfg.BaseUrl)
u.Path = path.Join(u.Path, "v4.0")
cfg.BaseUrl = u.String()
return &cfg, nil
}
func (w *WordsApiErrorResponse) Error() string {
out, err := json.Marshal(w)
if err != nil {
return err.Error()
}
return string(out)
}
type Duration struct {
time.Duration
}
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
d.Duration = time.Duration(value)
return nil
case string:
var err error
d.Duration, err = time.ParseDuration(value)
if err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}