forked from Teamwork/twapi-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
191 lines (164 loc) · 4.96 KB
/
types.go
File metadata and controls
191 lines (164 loc) · 4.96 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package twapi
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"
)
// HTTPError represents an error response from the API.
type HTTPError struct {
StatusCode int
Headers http.Header
Message string
Details string
}
// NewHTTPError creates a new HTTPError from an http.Response.
func NewHTTPError(resp *http.Response, message string) *HTTPError {
body := "no response body"
if b, err := io.ReadAll(resp.Body); err == nil && len(b) > 0 {
body = string(b)
}
return &HTTPError{
StatusCode: resp.StatusCode,
Headers: resp.Header,
Message: message,
Details: body,
}
}
// Error implements the error interface.
func (e *HTTPError) Error() string {
return fmt.Sprintf("%s (%d): %s", e.Message, e.StatusCode, e.Details)
}
// Relationship describes the relation between the main entity and a sideload type.
type Relationship struct {
ID int64 `json:"id"`
Type string `json:"type"`
Meta map[string]any `json:"meta,omitempty"`
}
// OptionalDateTime is a type alias for time.Time, used to represent date and
// time values in the API. The difference is that it will accept empty strings
// as valid values.
type OptionalDateTime time.Time
// MarshalJSON encodes the OptionalDateTime as a string in the format
// "2006-01-02T15:04:05Z07:00".
func (d OptionalDateTime) MarshalJSON() ([]byte, error) {
return time.Time(d).MarshalJSON()
}
// UnmarshalJSON decodes a JSON string into an OptionalDateTime type.
func (d *OptionalDateTime) UnmarshalJSON(data []byte) error {
if len(data) == 0 || string(data) == `""` || string(data) == "null" {
return nil
}
return (*time.Time)(d).UnmarshalJSON(data)
}
// Date is a type alias for time.Time, used to represent date values in the API.
type Date time.Time
// MarshalJSON encodes the Date as a string in the format "2006-01-02".
func (d Date) MarshalJSON() ([]byte, error) {
return []byte(`"` + time.Time(d).Format("2006-01-02") + `"`), nil
}
// UnmarshalJSON decodes a JSON string into a Date type.
func (d *Date) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
parsedTime, err := time.Parse("2006-01-02", str)
if err != nil {
return err
}
*d = Date(parsedTime)
return nil
}
// MarshalText encodes the Date as a string in the format "2006-01-02".
func (d Date) MarshalText() ([]byte, error) {
quotedValue, err := d.MarshalJSON()
if err != nil {
return nil, err
}
// it is expected that encoding.TextMarshaler does not return quotes.
value, err := strconv.Unquote(string(quotedValue))
if err != nil {
return nil, err
}
return []byte(value), nil
}
// UnmarshalText decodes a text string into a Date type. This is required when
// using Date type as a map key.
func (d *Date) UnmarshalText(text []byte) error {
return d.UnmarshalJSON(text)
}
// IsZero reports whether the Date is zero.
func (d Date) IsZero() bool {
return time.Time(d).IsZero()
}
// String returns the string representation of the Date in the format
// "2006-01-02".
func (d Date) String() string {
return time.Time(d).Format("2006-01-02")
}
// Time is a type alias for time.Time, used to represent time values in the API.
type Time time.Time
// MarshalJSON encodes the Time as a string in the format "15:04:05".
func (t Time) MarshalJSON() ([]byte, error) {
return []byte(`"` + time.Time(t).Format("15:04:05") + `"`), nil
}
// UnmarshalJSON decodes a JSON string into a Date type.
func (t *Time) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
parsedTime, err := time.Parse("15:04:05", str)
if err != nil {
return err
}
*t = Time(parsedTime)
return nil
}
// MarshalText encodes the Time as a string in the format "15:04:05".
func (t Time) MarshalText() ([]byte, error) {
quotedValue, err := t.MarshalJSON()
if err != nil {
return nil, err
}
// it is expected that encoding.TextMarshaler does not return quotes.
value, err := strconv.Unquote(string(quotedValue))
if err != nil {
return nil, err
}
return []byte(value), nil
}
// UnmarshalText decodes a text string into a Time type. This is required when
// using Time type as a map key.
func (t *Time) UnmarshalText(text []byte) error {
return t.UnmarshalJSON(text)
}
// String returns the string representation of the Time in the format
// "15:04:05".
func (t Time) String() string {
return time.Time(t).Format("15:04:05")
}
// Money represents a monetary value in the API.
type Money int64
// Set sets the value of Money from a float64.
func (m *Money) Set(value float64) {
*m = Money(value * 100)
}
// Value returns the value of Money as a float64.
func (m Money) Value() float64 {
return float64(m) / 100
}
// NewMoney creates a Money from a float64 value (in major units).
func NewMoney(value float64) Money {
return Money(value * 100)
}
// OrderMode specifies the order direction (asc, desc).
type OrderMode string
// Supported order modes.
const (
OrderModeAscending OrderMode = "asc"
OrderModeDescending OrderMode = "desc"
)