-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.go
65 lines (55 loc) · 1.58 KB
/
models.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
package domainavailability
import (
"encoding/json"
"fmt"
)
// unmarshalString parses the JSON-encoded data and returns value as a string.
func unmarshalString(raw json.RawMessage) (string, error) {
var val string
err := json.Unmarshal(raw, &val)
if err != nil {
return "", err
}
return val, nil
}
// StringBool is a helper wrapper on bool
type StringBool bool
// UnmarshalJSON decodes AVAILABLE/UNAVAILABLE values from Domain Availability API.
func (b *StringBool) UnmarshalJSON(bytes []byte) error {
str, err := unmarshalString(bytes)
if err != nil {
return err
}
switch str {
case "AVAILABLE":
*b = true
case "UNAVAILABLE":
*b = false
default:
return &ErrorMessage{"", `"` + str + `"` + " is unexpected value for domainAvailability"}
}
return nil
}
// MarshalJSON encodes AVAILABLE/UNAVAILABLE values to the Domain Availability API representation.
func (b *StringBool) MarshalJSON() ([]byte, error) {
if *b {
return []byte(`"AVAILABLE"`), nil
}
return []byte(`"UNAVAILABLE"`), nil
}
// DomainAvailabilityResponse is a response of Domain Availability API.
type DomainAvailabilityResponse struct {
// DomainName is the target domain name.
DomainName string `json:"domainName"`
// IsAvailable is the registration state of the domain name.
IsAvailable *StringBool `json:"domainAvailability"`
}
// ErrorMessage is the error message.
type ErrorMessage struct {
Code string `json:"errorCode"`
Message string `json:"msg"`
}
// Error returns error message as a string.
func (e *ErrorMessage) Error() string {
return fmt.Sprintf("API error: [%s] %s", e.Code, e.Message)
}