1
|
|
|
package mollie |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"encoding/json" |
5
|
|
|
"fmt" |
6
|
|
|
"net/http" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
// PermissionGrant defines supported permissions. |
10
|
|
|
type PermissionGrant string |
11
|
|
|
|
12
|
|
|
// Available permission grants. |
13
|
|
|
const ( |
14
|
|
|
PaymentsRead PermissionGrant = "payments.read" |
15
|
|
|
PaymentsWrite PermissionGrant = "payments.write" |
16
|
|
|
RefundsRead PermissionGrant = "refunds.read" |
17
|
|
|
RefundsWrite PermissionGrant = "refunds.write" |
18
|
|
|
CustomersRead PermissionGrant = "customers.read" |
19
|
|
|
CustomersWrite PermissionGrant = "customers.write" |
20
|
|
|
MandatesRead PermissionGrant = "mandates.read" |
21
|
|
|
MandatesWrite PermissionGrant = "mandates.write" |
22
|
|
|
SubscriptionsRead PermissionGrant = "subscriptions.read" |
23
|
|
|
SubscriptionsWrite PermissionGrant = "subscriptions.write" |
24
|
|
|
ProfilesRead PermissionGrant = "profiles.read" |
25
|
|
|
ProfilesWrite PermissionGrant = "profiles.write" |
26
|
|
|
InvoicesRead PermissionGrant = "invoices.read" |
27
|
|
|
OrdersRead PermissionGrant = "orders.read" |
28
|
|
|
OrdersWrite PermissionGrant = "orders.write" |
29
|
|
|
ShipmentsRead PermissionGrant = "shipments.read" |
30
|
|
|
ShipmentsWrite PermissionGrant = "shipments.write" |
31
|
|
|
OrganizationsRead PermissionGrant = "organizations.read" |
32
|
|
|
OrganizationsWrite PermissionGrant = "organizations.write" |
33
|
|
|
OnboardingRead PermissionGrant = "onbording.read" |
34
|
|
|
OnboardingWrite PermissionGrant = "onbording.write" |
35
|
|
|
) |
36
|
|
|
|
37
|
|
|
// Permission represents an action that |
38
|
|
|
// can be performed by any API actor. |
39
|
|
|
type Permission struct { |
40
|
|
|
Resource string `json:"resource,omitempty"` |
41
|
|
|
ID PermissionGrant `json:"id,omitempty"` |
42
|
|
|
Description string `json:"description,omitempty"` |
43
|
|
|
Granted bool `json:"granted,omitempty"` |
44
|
|
|
Links PermissionLinks `json:"_links,omitempty"` |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// PermissionLinks contains URL objects that make |
48
|
|
|
// reference to an http addres related to permissions. |
49
|
|
|
type PermissionLinks struct { |
50
|
|
|
Self *URL `json:"self,omitempty"` |
51
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// PermissionsList lists all the permissions given to an |
55
|
|
|
// API actor. |
56
|
|
|
type PermissionsList struct { |
57
|
|
|
Count int `json:"count,omitempty"` |
58
|
|
|
Embedded struct { |
59
|
|
|
Permissions []*Permission `json:"permissions,omitempty"` |
60
|
|
|
} `json:"_embedded,omitempty"` |
61
|
|
|
Links PermissionLinks `json:"_links,omitempty"` |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// PermissionsService operates over permission resources. |
65
|
|
|
type PermissionsService service |
66
|
|
|
|
67
|
|
|
// Get returns a permission by its id. |
68
|
|
|
// |
69
|
|
|
// See: https://docs.mollie.com/reference/v2/permissions-api/get-permission |
70
|
|
|
func (ps *PermissionsService) Get(id string) (p *Permission, err error) { |
71
|
1 |
|
u := fmt.Sprintf("v2/permissions/%s", id) |
72
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodGet, u, nil) |
73
|
1 |
|
if err != nil { |
74
|
1 |
|
return |
75
|
|
|
} |
76
|
1 |
|
res, err := ps.client.Do(req) |
77
|
1 |
|
if err != nil { |
78
|
1 |
|
return |
79
|
|
|
} |
80
|
1 |
|
if err = json.Unmarshal(res.content, &p); err != nil { |
81
|
1 |
|
return |
82
|
|
|
} |
83
|
1 |
|
return |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// List retrieves all permissions available with the current app access token. |
87
|
|
|
// The list is not paginated. |
88
|
|
|
// |
89
|
|
|
// See: https://docs.mollie.com/reference/v2/permissions-api/list-permissions |
90
|
|
|
func (ps *PermissionsService) List() (pl *PermissionsList, err error) { |
91
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodGet, "v2/permissions", nil) |
92
|
1 |
|
if err != nil { |
93
|
1 |
|
return |
94
|
|
|
} |
95
|
1 |
|
res, err := ps.client.Do(req) |
96
|
1 |
|
if err != nil { |
97
|
1 |
|
return |
98
|
|
|
} |
99
|
1 |
|
if err = json.Unmarshal(res.content, &pl); err != nil { |
100
|
1 |
|
return |
101
|
|
|
} |
102
|
1 |
|
return |
103
|
|
|
} |
104
|
|
|
|