Skip to content

Commit

Permalink
Refactor Client Code, parts 1 and 2 of several (edgexfoundry#195, edg…
Browse files Browse the repository at this point in the history
…exfoundry#197)


Signed-off-by: Brandon Forster <brandonforster@gmail.com>
  • Loading branch information
brandonforster authored and michaelestrin committed Jan 16, 2020
1 parent 93abd0d commit 6abf550
Show file tree
Hide file tree
Showing 28 changed files with 798 additions and 296 deletions.
5 changes: 3 additions & 2 deletions clients/command/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"

"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"
)

Expand All @@ -38,11 +39,11 @@ type CommandClient interface {

type commandRestClient struct {
url string
endpoint clients.Endpointer
endpoint interfaces.Endpointer
}

// NewCommandClient creates an instance of CommandClient
func NewCommandClient(params types.EndpointParams, m clients.Endpointer) CommandClient {
func NewCommandClient(params types.EndpointParams, m interfaces.Endpointer) CommandClient {
c := commandRestClient{endpoint: m}
c.init(params)
return &c
Expand Down
5 changes: 3 additions & 2 deletions clients/coredata/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strconv"

"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/models"
)
Expand Down Expand Up @@ -68,11 +69,11 @@ type EventClient interface {

type eventRestClient struct {
url string
endpoint clients.Endpointer
endpoint interfaces.Endpointer
}

// NewEventClient creates an instance of EventClient
func NewEventClient(params types.EndpointParams, m clients.Endpointer) EventClient {
func NewEventClient(params types.EndpointParams, m interfaces.Endpointer) EventClient {
e := eventRestClient{endpoint: m}
e.init(params)
return &e
Expand Down
5 changes: 3 additions & 2 deletions clients/coredata/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strconv"

"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/models"
)
Expand Down Expand Up @@ -58,11 +59,11 @@ type ReadingClient interface {

type readingRestClient struct {
url string
endpoint clients.Endpointer
endpoint interfaces.Endpointer
}

// NewReadingClient creates an instance of a ReadingClient
func NewReadingClient(params types.EndpointParams, m clients.Endpointer) ReadingClient {
func NewReadingClient(params types.EndpointParams, m interfaces.Endpointer) ReadingClient {
r := readingRestClient{endpoint: m}
r.init(params)
return &r
Expand Down
5 changes: 3 additions & 2 deletions clients/coredata/value_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"

"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/models"
)
Expand Down Expand Up @@ -56,10 +57,10 @@ type ValueDescriptorClient interface {

type valueDescriptorRestClient struct {
url string
endpoint clients.Endpointer
endpoint interfaces.Endpointer
}

func NewValueDescriptorClient(params types.EndpointParams, m clients.Endpointer) ValueDescriptorClient {
func NewValueDescriptorClient(params types.EndpointParams, m interfaces.Endpointer) ValueDescriptorClient {
v := valueDescriptorRestClient{endpoint: m}
v.init(params)
return &v
Expand Down
5 changes: 3 additions & 2 deletions clients/general/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"

"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"
)

Expand All @@ -34,11 +35,11 @@ type GeneralClient interface {

type generalRestClient struct {
url string
endpoint clients.Endpointer
endpoint interfaces.Endpointer
}

// NewGeneralClient creates an instance of GeneralClient
func NewGeneralClient(params types.EndpointParams, m clients.Endpointer) GeneralClient {
func NewGeneralClient(params types.EndpointParams, m interfaces.Endpointer) GeneralClient {
gc := generalRestClient{endpoint: m}
gc.init(params)
return &gc
Expand Down
14 changes: 7 additions & 7 deletions clients/endpointer.go → clients/interfaces/endpointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
* the License.
*******************************************************************************/

package clients
package interfaces

import "github.com/edgexfoundry/go-mod-core-contracts/clients/types"

//Endpointer is the interface for types that need to implement or simulate integration
//with a service discovery provider.
// Endpointer is the interface for types that need to implement or simulate integration
// with a service discovery provider.
type Endpointer interface {
//Monitor is responsible for looking up information about the service endpoint corresponding
//to the params.ServiceKey property. The name "Monitor" implies that this lookup will be done
//at a regular interval. Information about the service from the discovery provider should be
//used to construct a URL which will then be pushed to the returned channel.
// Monitor is responsible for looking up information about the service endpoint corresponding
// to the params.ServiceKey property. The name "Monitor" implies that this lookup will be done
// at a regular interval. Information about the service from the discovery provider should be
// used to construct a URLClient which will then be pushed to the returned channel.
Monitor(params types.EndpointParams) chan string
}
26 changes: 26 additions & 0 deletions clients/interfaces/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*******************************************************************************
* Copyright 2020 Dell Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/

package interfaces

// URLClient is the interface for types that need to define some way to retrieve URLClient information about services.
// This information can be anything that must be determined at runtime, whether it is unknown or simply not yet known.
type URLClient interface {
// Prefix returns the URLClient base path (or root) of a service.
// This is the common root of all REST calls to the service,
// and is defined on a per service (rather than per endpoint) basis.
// Prefix returns the root URLClient for REST calls to the service if it was able to retrieve that URLClient;
// it returns an error otherwise.
Prefix() (string, error)
}
65 changes: 33 additions & 32 deletions clients/metadata/addressable.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
* the License.
*******************************************************************************/

/*
Package metadata provides clients used for integration with the core-metadata service.
*/
// metadata provides clients used for integration with the core-metadata service.
package metadata

import (
Expand All @@ -23,13 +21,13 @@ import (
"net/url"

"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"
"github.com/edgexfoundry/go-mod-core-contracts/models"
)

/*
AddressableClient defines the interface for interactions with the Addressable endpoint on the EdgeX Foundry core-metadata service.
*/
// AddressableClient defines the interface for interactions with the Addressable endpoint on core-metadata.
type AddressableClient interface {
// Add creates a new Addressable and returns the ID of the new item if successful.
Add(addr *models.Addressable, ctx context.Context) (string, error)
Expand All @@ -44,35 +42,23 @@ type AddressableClient interface {
}

type addressableRestClient struct {
url string
endpoint clients.Endpointer
urlClient interfaces.URLClient
}

// NewAddressableClient creates an instance of AddressableClient
func NewAddressableClient(params types.EndpointParams, m clients.Endpointer) AddressableClient {
a := addressableRestClient{endpoint: m}
a.init(params)
func NewAddressableClient(params types.EndpointParams, m interfaces.Endpointer) AddressableClient {
a := addressableRestClient{urlClient: urlclient.New(params, m)}
return &a
}

func (a *addressableRestClient) init(params types.EndpointParams) {
if params.UseRegistry {
go func(ch chan string) {
for {
select {
case url := <-ch:
a.url = url
}
}
}(a.endpoint.Monitor(params))
} else {
a.url = params.Url
// Helper method to request and decode an addressable
func (a *addressableRestClient) requestAddressable(urlSuffix string, ctx context.Context) (models.Addressable, error) {
urlPrefix, err := a.urlClient.Prefix()
if err != nil {
return models.Addressable{}, err
}
}

// Helper method to request and decode an addressable
func (a *addressableRestClient) requestAddressable(url string, ctx context.Context) (models.Addressable, error) {
data, err := clients.GetRequest(url, ctx)
data, err := clients.GetRequest(urlPrefix+urlSuffix, ctx)
if err != nil {
return models.Addressable{}, err
}
Expand All @@ -83,21 +69,36 @@ func (a *addressableRestClient) requestAddressable(url string, ctx context.Conte
}

func (a *addressableRestClient) Add(addr *models.Addressable, ctx context.Context) (string, error) {
return clients.PostJsonRequest(a.url, addr, ctx)
serviceURL, err := a.urlClient.Prefix()
if err != nil {
return "", err
}

return clients.PostJsonRequest(serviceURL, addr, ctx)
}

func (a *addressableRestClient) Addressable(id string, ctx context.Context) (models.Addressable, error) {
return a.requestAddressable(a.url+"/"+id, ctx)
return a.requestAddressable("/"+id, ctx)
}

func (a *addressableRestClient) AddressableForName(name string, ctx context.Context) (models.Addressable, error) {
return a.requestAddressable(a.url+"/name/"+url.QueryEscape(name), ctx)
return a.requestAddressable("/name/"+url.QueryEscape(name), ctx)
}

func (a *addressableRestClient) Update(addr models.Addressable, ctx context.Context) error {
return clients.UpdateRequest(a.url, addr, ctx)
serviceURL, err := a.urlClient.Prefix()
if err != nil {
return err
}

return clients.UpdateRequest(serviceURL, addr, ctx)
}

func (a *addressableRestClient) Delete(id string, ctx context.Context) error {
return clients.DeleteRequest(a.url+"/id/"+id, ctx)
serviceURL, err := a.urlClient.Prefix()
if err != nil {
return err
}

return clients.DeleteRequest(serviceURL+"/id/"+id, ctx)
}
23 changes: 12 additions & 11 deletions clients/metadata/addressable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/google/uuid"

"github.com/edgexfoundry/go-mod-core-contracts/clients"
"github.com/edgexfoundry/go-mod-core-contracts/clients/types"
"github.com/edgexfoundry/go-mod-core-contracts/models"
"github.com/google/uuid"
)

func TestNewAddressableClientWithConsul(t *testing.T) {
Expand All @@ -44,15 +44,16 @@ func TestNewAddressableClientWithConsul(t *testing.T) {
t.Error("sc is not of expected type")
}

time.Sleep(25 * time.Millisecond)
if len(r.url) == 0 {
url, err := r.urlClient.Prefix()

if err != nil {
t.Error("url was not initialized")
} else if r.url != addressableURL {
t.Errorf("unexpected url value %s", r.url)
} else if url != addressableURL {
t.Errorf("unexpected url value %s", url)
}
}

// Test adding an addressable using the client
// Test adding an addressable using the urlClient
func TestAddAddressable(t *testing.T) {
addressable := models.Addressable{
Id: uuid.New().String(),
Expand Down Expand Up @@ -98,7 +99,7 @@ func TestAddAddressable(t *testing.T) {
}
}

// Test get an addressable using the client
// Test get an addressable using the urlClient
func TestGetAddressable(t *testing.T) {
addressable := models.Addressable{
Id: uuid.New().String(),
Expand Down Expand Up @@ -143,7 +144,7 @@ func TestGetAddressable(t *testing.T) {
}
}

// Test get an addressable using the client
// Test get an addressable using the urlClient
func TestGetAddressableForName(t *testing.T) {
addressable := models.Addressable{
Id: uuid.New().String(),
Expand Down Expand Up @@ -188,7 +189,7 @@ func TestGetAddressableForName(t *testing.T) {
}
}

// Test updating an addressable using the client
// Test updating an addressable using the urlClient
func TestUpdateAddressable(t *testing.T) {
addressable := models.Addressable{
Id: uuid.New().String(),
Expand Down Expand Up @@ -226,7 +227,7 @@ func TestUpdateAddressable(t *testing.T) {
}
}

// Test deleting an addressable using the client
// Test deleting an addressable using the urlClient
func TestDeleteAddressable(t *testing.T) {
addressable := models.Addressable{
Id: uuid.New().String(),
Expand Down
Loading

0 comments on commit 6abf550

Please sign in to comment.