-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from gildas/release/0.9.7
Merge release/0.9.7
- Loading branch information
Showing
3 changed files
with
132 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters