-
Notifications
You must be signed in to change notification settings - Fork 1
/
external_contact.go
87 lines (79 loc) · 2.29 KB
/
external_contact.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
package gcloudcx
import (
"context"
"encoding/json"
"github.com/gildas/go-errors"
"github.com/gildas/go-logger"
"github.com/google/uuid"
)
// ExternalContact represents an external contact
//
// See: https://developer.genesys.cloud/devapps/api-explorer#post-api-v2-externalcontacts-contacts
type ExternalContact struct {
ID uuid.UUID `json:"id"`
SelfURI URI `json:"selfUri,omitempty"`
Name string `json:"name"`
client *Client `json:"-"`
logger *logger.Logger `json:"-"`
}
// Initialize initializes the object
//
// accepted parameters: *gcloudcx.Client, *logger.Logger
//
// implements Initializable
func (contact *ExternalContact) Initialize(parameters ...interface{}) {
for _, raw := range parameters {
switch parameter := raw.(type) {
case uuid.UUID:
contact.ID = parameter
case *Client:
contact.client = parameter
case *logger.Logger:
contact.logger = parameter.Child("conversation", "conversation", "id", contact.ID)
}
}
if contact.logger == nil {
contact.logger = logger.Create("gclouccx", &logger.NilStream{})
}
}
// GetID gets the identifier of this
//
// implements Identifiable
func (contact ExternalContact) GetID() uuid.UUID {
return contact.ID
}
// GetURI gets the URI of this
//
// implements Addressable
func (contact ExternalContact) GetURI(ids ...uuid.UUID) URI {
if len(ids) > 0 {
return NewURI("/api/v2/externalcontacts/contacts/%s", ids[0])
}
if contact.ID != uuid.Nil {
return NewURI("/api/v2/externalcontacts/contacts/%s", contact.ID)
}
return URI("/api/v2/externalcontacts/contacts/")
}
// String gets a string version
//
// implements the fmt.Stringer interface
func (contact ExternalContact) String() string {
if len(contact.Name) != 0 {
return contact.Name
}
return contact.ID.String()
}
// SearchExternalContact search for an external contact by one of its attributes
func (client *Client) SearchExternalContact(context context.Context, value string) (contact *ExternalContact, err error) {
entities, err := client.FetchEntities(context, NewURI("/externalcontacts/contacts").WithQuery(Query{"q": value}))
if err != nil {
return nil, err
}
if len(entities) == 0 {
return nil, errors.NotFound.With("value", value)
}
if err = json.Unmarshal(entities[0], &contact); err != nil {
return nil, err
}
return
}