Skip to content

Commit

Permalink
edgexfoundry#196: Applied URLClient pattern to notification.
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Forster <me@brandonforster.com>
  • Loading branch information
brandonforster committed Jan 21, 2020
1 parent 17a7138 commit 19cdcb0
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions clients/notifications/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
* the License.
*******************************************************************************/

/*
Package notifications provides a client for integrating with the support-notifications service.
*/
// notifications provides a client for integrating with the support-notifications service.
package notifications

import (
Expand All @@ -23,6 +21,7 @@ import (
"github.com/edgexfoundry/go-mod-core-contracts/clients"
"github.com/edgexfoundry/go-mod-core-contracts/clients/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/clients/types"
"github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient"
)

type CategoryEnum string
Expand All @@ -48,18 +47,15 @@ const (
ESCALATED StatusEnum = "ESCALATED"
)

/*
NotificationsClient defines the interface for interactions with the EdgeX Foundry support-notifications service.
*/
// NotificationsClient defines the interface for interactions with the EdgeX Foundry support-notifications service.
type NotificationsClient interface {
// SendNotification sends a notification.
SendNotification(n Notification, ctx context.Context) error
}

// Type struct for REST-specific implementation of the NotificationsClient interface
type notificationsRestClient struct {
url string
endpoint interfaces.Endpointer
urlClient interfaces.URLClient
}

// Notification defines the structure of data being sent.
Expand All @@ -79,27 +75,15 @@ type Notification struct {

// NewNotificationsClient creates an instance of NotificationsClient
func NewNotificationsClient(params types.EndpointParams, m interfaces.Endpointer) NotificationsClient {
n := notificationsRestClient{endpoint: m}
n.init(params)
return &n
return &notificationsRestClient{urlClient: urlclient.New(params, m)}
}

func (n *notificationsRestClient) init(params types.EndpointParams) {
if params.UseRegistry {
go func(ch chan string) {
for {
select {
case url := <-ch:
n.url = url
}
}
}(n.endpoint.Monitor(params))
} else {
n.url = params.Url
func (nc *notificationsRestClient) SendNotification(n Notification, ctx context.Context) error {
url, err := nc.urlClient.Prefix()
if err != nil {
return err
}
}

func (nc *notificationsRestClient) SendNotification(n Notification, ctx context.Context) error {
_, err := clients.PostJsonRequest(nc.url, n, ctx)
_, err = clients.PostJsonRequest(url, n, ctx)
return err
}

0 comments on commit 19cdcb0

Please sign in to comment.