-
Notifications
You must be signed in to change notification settings - Fork 13
/
code.go
104 lines (92 loc) · 2.06 KB
/
code.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
package mixin
import (
"context"
"encoding/json"
"fmt"
)
type CodeType string
const (
TypeUser CodeType = "user"
TypeConversation CodeType = "conversation"
TypePayment CodeType = "payment"
TypeMultisig CodeType = "multisig_request"
TypeCollectible CodeType = "non_fungible_request"
TypeAuthorization CodeType = "authorization"
)
type Code struct {
Type CodeType `json:"type"`
RawData json.RawMessage
}
func (c *Code) User() *User {
if c.Type != TypeUser {
return nil
}
var user User
if err := json.Unmarshal(c.RawData, &user); err != nil {
return nil
}
return &user
}
func (c *Code) Conversation() *Conversation {
if c.Type != TypeConversation {
return nil
}
var conversation Conversation
if err := json.Unmarshal(c.RawData, &conversation); err != nil {
return nil
}
return &conversation
}
func (c *Code) Payment() *Payment {
if c.Type != TypePayment {
return nil
}
var payment Payment
if err := json.Unmarshal(c.RawData, &payment); err != nil {
return nil
}
return &payment
}
func (c *Code) Multisig() *MultisigRequest {
if c.Type != TypeMultisig {
return nil
}
var multisig MultisigRequest
if err := json.Unmarshal(c.RawData, &multisig); err != nil {
return nil
}
return &multisig
}
func (c *Code) Collectible() *CollectibleRequest {
if c.Type != TypeCollectible {
return nil
}
var collectible CollectibleRequest
if err := json.Unmarshal(c.RawData, &collectible); err != nil {
return nil
}
return &collectible
}
func (c *Code) Authorization() *Authorization {
if c.Type != TypeAuthorization {
return nil
}
var authorization Authorization
if err := json.Unmarshal(c.RawData, &authorization); err != nil {
return nil
}
return &authorization
}
func (c *Client) GetCode(ctx context.Context, codeString string) (*Code, error) {
uri := fmt.Sprintf("/codes/%s", codeString)
var data json.RawMessage
if err := c.Get(ctx, uri, nil, &data); err != nil {
return nil, err
}
var code Code
if err := json.Unmarshal(data, &code); err != nil {
return nil, err
}
code.RawData = data
return &code, nil
}