Repository navigation
Expand file tree
/
Copy pathcustom_objects.go
More file actions
154 lines (138 loc) · 6.14 KB
/
Copy pathcustom_objects.go
File metadata and controls
154 lines (138 loc) · 6.14 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
package intercom
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
gen "github.com/uffejaeger/intercom-go/internal/generated/intercom"
)
// CustomObjectInstance is an Intercom custom object instance.
type CustomObjectInstance = gen.CustomObjectInstanceSchema
// CustomObjectInstanceDeleted is a deleted Intercom custom object instance response.
type CustomObjectInstanceDeleted = gen.CustomObjectInstanceDeletedSchema
// CustomObjectInstanceCreateOrUpdate holds the fields for creating or updating a custom object instance.
type CustomObjectInstanceCreateOrUpdate = gen.CreateOrUpdateCustomObjectInstanceRequestSchema
// CustomObjectInstanceList is a paginated list of custom object instances.
type CustomObjectInstanceList = gen.CustomObjectInstancesPaginatedListSchema
// CustomObjectInstanceListParams configures a custom object instance list request.
//
// External-ID lookup is intentionally not part of this type. The API returns a
// single instance, rather than a list, when external_id is supplied; use
// GetByExternalID for that response shape.
type CustomObjectInstanceListParams struct {
ReferencesContactId *string
ReferencesConversationId *string
Page *int
PerPage *int
}
func (p *CustomObjectInstanceListParams) toGenerated() *gen.ListCustomObjectInstancesParams {
if p == nil {
return nil
}
return &gen.ListCustomObjectInstancesParams{
ReferencesContactId: p.ReferencesContactId,
ReferencesConversationId: p.ReferencesConversationId,
Page: p.Page,
PerPage: p.PerPage,
}
}
// CustomObjectsService exposes custom-object instance Intercom API operations.
type CustomObjectsService struct {
client *Client
}
// List returns instances for a custom object type.
func (s *CustomObjectsService) List(ctx context.Context, customObjectType string, params *CustomObjectInstanceListParams) (*CustomObjectInstanceList, error) {
if err := requireCustomObjectType(customObjectType); err != nil {
return nil, err
}
res, err := s.client.generated.ListCustomObjectInstancesWithResponse(ctx, customObjectType, params.toGenerated())
if err != nil {
return nil, err
}
return requireOK("list custom object instances", res.StatusCode(), res.Body, res.JSON200, responseHeaders(res.HTTPResponse))
}
// CreateOrUpdate creates or updates a custom object instance for a custom object type.
func (s *CustomObjectsService) CreateOrUpdate(ctx context.Context, customObjectType string, instance CustomObjectInstanceCreateOrUpdate) (*CustomObjectInstance, error) {
if err := requireCustomObjectType(customObjectType); err != nil {
return nil, err
}
res, err := s.client.generated.CreateCustomObjectInstancesWithResponse(ctx, customObjectType, nil, gen.CreateCustomObjectInstancesJSONRequestBody(instance))
if err != nil {
return nil, err
}
return requireOK("create or update custom object instance", res.StatusCode(), res.Body, res.JSON200, responseHeaders(res.HTTPResponse))
}
// Get returns a custom object instance by Intercom ID.
func (s *CustomObjectsService) Get(ctx context.Context, customObjectType, instanceID string) (*CustomObjectInstance, error) {
if err := requireCustomObjectType(customObjectType); err != nil {
return nil, err
}
if instanceID == "" {
return nil, fmt.Errorf("intercom: custom object instance ID is required")
}
res, err := s.client.generated.GetCustomObjectInstancesByIdWithResponse(ctx, customObjectType, instanceID, nil)
if err != nil {
return nil, err
}
return requireOK("get custom object instance", res.StatusCode(), res.Body, res.JSON200, responseHeaders(res.HTTPResponse))
}
// GetByExternalID returns a custom object instance by external ID.
func (s *CustomObjectsService) GetByExternalID(ctx context.Context, customObjectType, externalID string) (*CustomObjectInstance, error) {
if err := requireCustomObjectType(customObjectType); err != nil {
return nil, err
}
if externalID == "" {
return nil, fmt.Errorf("intercom: custom object instance external ID is required")
}
path := "/custom_object_instances/" + url.PathEscape(customObjectType) + "?external_id=" + url.QueryEscape(externalID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
res, err := s.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("intercom: read custom object instance by external ID response: %w", err)
}
return requireJSON[CustomObjectInstance]("get custom object instance by external ID", res.StatusCode, body, res.Header)
}
// Delete deletes a custom object instance by Intercom ID.
func (s *CustomObjectsService) Delete(ctx context.Context, customObjectType, instanceID string) (*CustomObjectInstanceDeleted, error) {
if err := requireCustomObjectType(customObjectType); err != nil {
return nil, err
}
if instanceID == "" {
return nil, fmt.Errorf("intercom: custom object instance ID is required")
}
res, err := s.client.generated.DeleteCustomObjectInstancesByExternalIdWithResponse(ctx, customObjectType, instanceID, nil)
if err != nil {
return nil, err
}
return requireOK("delete custom object instance", res.StatusCode(), res.Body, res.JSON200, responseHeaders(res.HTTPResponse))
}
// DeleteByExternalID deletes a custom object instance by external ID.
func (s *CustomObjectsService) DeleteByExternalID(ctx context.Context, customObjectType, externalID string) (*CustomObjectInstanceDeleted, error) {
if err := requireCustomObjectType(customObjectType); err != nil {
return nil, err
}
if externalID == "" {
return nil, fmt.Errorf("intercom: custom object instance external ID is required")
}
params := &gen.DeleteCustomObjectInstancesByIdParams{ExternalId: externalID}
res, err := s.client.generated.DeleteCustomObjectInstancesByIdWithResponse(ctx, customObjectType, params)
if err != nil {
return nil, err
}
return requireOK("delete custom object instance by external ID", res.StatusCode(), res.Body, res.JSON200, responseHeaders(res.HTTPResponse))
}
func requireCustomObjectType(customObjectType string) error {
if customObjectType == "" {
return fmt.Errorf("intercom: custom object type is required")
}
return nil
}