Skip to content

Commit

Permalink
edgexfoundry#206: Refactored so context is the first parameter in eve…
Browse files Browse the repository at this point in the history
…ry client it's used in.

Signed-off-by: Brandon Forster <me@brandonforster.com>
  • Loading branch information
brandonforster committed Feb 1, 2020
1 parent 5010e33 commit 28e9f23
Show file tree
Hide file tree
Showing 19 changed files with 194 additions and 214 deletions.
79 changes: 36 additions & 43 deletions clients/coredata/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,31 @@ type EventClient interface {
// Events gets a list of all events
Events(ctx context.Context) ([]models.Event, error)
// Event gets an event by its id
Event(id string, ctx context.Context) (models.Event, error)
Event(ctx context.Context, id string) (models.Event, error)
// EventCount returns the total count of events
EventCount(ctx context.Context) (int, error)
// EventCountForDevice returns the total event count for the specified device
EventCountForDevice(deviceId string, ctx context.Context) (int, error)
EventCountForDevice(ctx context.Context, deviceId string) (int, error)
// EventsForDevice returns events up to a specified number that were generated by a given device
EventsForDevice(id string, limit int, ctx context.Context) ([]models.Event, error)
EventsForDevice(ctx context.Context, id string, limit int) ([]models.Event, error)
// EventsForInterval returns events generated within a specific time period
EventsForInterval(start int, end int, limit int, ctx context.Context) ([]models.Event, error)
EventsForInterval(ctx context.Context, start int, end int, limit int) ([]models.Event, error)
// EventsForDeviceAndValueDescriptor returns events for the specified device and value descriptor
EventsForDeviceAndValueDescriptor(deviceId string, vd string, limit int, ctx context.Context) ([]models.Event, error)
EventsForDeviceAndValueDescriptor(ctx context.Context, deviceId string, vd string, limit int) ([]models.Event, error)
// Add will post a new event
Add(event *models.Event, ctx context.Context) (string, error)
Add(ctx context.Context, event *models.Event) (string, error)
// AddBytes posts a new event using an array of bytes, supporting encoding of the event by the caller.
AddBytes(event []byte, ctx context.Context) (string, error)
AddBytes(ctx context.Context, event []byte) (string, error)
// DeleteForDevice will delete events by the specified device name
DeleteForDevice(id string, ctx context.Context) error
DeleteForDevice(ctx context.Context, id string) error
// DeleteOld deletes events according to their age
DeleteOld(age int, ctx context.Context) error
DeleteOld(ctx context.Context, age int) error
// Delete will delete an event by its id
Delete(id string, ctx context.Context) error
Delete(ctx context.Context, id string) error
// MarkPushed designates an event as having been successfully exported
MarkPushed(id string, ctx context.Context) error
MarkPushed(ctx context.Context, id string) error
// MarkPushedByChecksum designates an event as having been successfully exported using a checksum for the respective event.
MarkPushedByChecksum(checksum string, ctx context.Context) error
MarkPushedByChecksum(ctx context.Context, checksum string) error
// MarshalEvent will perform JSON or CBOR encoding of the supplied Event. If one or more Readings on the Event
// has a populated BinaryValue, the marshaling will be CBOR. Default is JSON.
MarshalEvent(e models.Event) ([]byte, error)
Expand All @@ -76,7 +76,7 @@ func NewEventClient(urlClient interfaces.URLClient) EventClient {
}

// Helper method to request and decode an event slice
func (e *eventRestClient) requestEventSlice(urlSuffix string, ctx context.Context) ([]models.Event, error) {
func (e *eventRestClient) requestEventSlice(ctx context.Context, urlSuffix string) ([]models.Event, error) {
data, err := clients.GetRequest(ctx, urlSuffix, e.urlClient)
if err != nil {
return []models.Event{}, err
Expand All @@ -92,7 +92,7 @@ func (e *eventRestClient) requestEventSlice(urlSuffix string, ctx context.Contex
}

// Helper method to request and decode an event
func (e *eventRestClient) requestEvent(urlSuffix string, ctx context.Context) (models.Event, error) {
func (e *eventRestClient) requestEvent(ctx context.Context, urlSuffix string) (models.Event, error) {
data, err := clients.GetRequest(ctx, urlSuffix, e.urlClient)
if err != nil {
return models.Event{}, err
Expand All @@ -104,77 +104,70 @@ func (e *eventRestClient) requestEvent(urlSuffix string, ctx context.Context) (m
}

func (e *eventRestClient) Events(ctx context.Context) ([]models.Event, error) {
return e.requestEventSlice("", ctx)
return e.requestEventSlice(ctx, "")
}

func (e *eventRestClient) Event(id string, ctx context.Context) (models.Event, error) {
return e.requestEvent("/"+id, ctx)
func (e *eventRestClient) Event(ctx context.Context, id string) (models.Event, error) {
return e.requestEvent(ctx, "/"+id)
}

func (e *eventRestClient) EventCount(ctx context.Context) (int, error) {
return clients.CountRequest(ctx, "/count", e.urlClient)
}

func (e *eventRestClient) EventCountForDevice(deviceId string, ctx context.Context) (int, error) {
func (e *eventRestClient) EventCountForDevice(ctx context.Context, deviceId string) (int, error) {
return clients.CountRequest(ctx, "/count/"+url.QueryEscape(deviceId), e.urlClient)
}

func (e *eventRestClient) EventsForDevice(deviceId string, limit int, ctx context.Context) ([]models.Event, error) {
return e.requestEventSlice("/device/"+url.QueryEscape(deviceId)+"/"+strconv.Itoa(limit), ctx)
func (e *eventRestClient) EventsForDevice(ctx context.Context, id string, limit int) ([]models.Event, error) {
return e.requestEventSlice(ctx, "/device/"+url.QueryEscape(deviceId)+"/"+strconv.Itoa(limit))
}

func (e *eventRestClient) EventsForInterval(start int, end int, limit int, ctx context.Context) ([]models.Event, error) {
return e.requestEventSlice("/"+strconv.Itoa(start)+"/"+strconv.Itoa(end)+"/"+strconv.Itoa(limit), ctx)
func (e *eventRestClient) EventsForInterval(ctx context.Context, start int, end int, limit int) ([]models.Event, error) {
return e.requestEventSlice(ctx, "/"+strconv.Itoa(start)+"/"+strconv.Itoa(end)+"/"+strconv.Itoa(limit))
}

func (e *eventRestClient) EventsForDeviceAndValueDescriptor(
deviceId string,
vd string,
limit int,
ctx context.Context) ([]models.Event, error) {
func (e *eventRestClient) EventsForDeviceAndValueDescriptor(ctx context.Context, deviceId string, vd string, limit int) ([]models.Event, error) {

return e.requestEventSlice(
"/device/"+
url.QueryEscape(deviceId)+
"/valuedescriptor/"+
url.QueryEscape(vd)+
"/"+strconv.Itoa(limit),
ctx,
)
return e.requestEventSlice(ctx, "/device/"+
url.QueryEscape(deviceId)+
"/valuedescriptor/"+
url.QueryEscape(vd)+
"/"+strconv.Itoa(limit), )
}

func (e *eventRestClient) Add(event *models.Event, ctx context.Context) (string, error) {
func (e *eventRestClient) Add(ctx context.Context, event *models.Event) (string, error) {
content := clients.FromContext(clients.ContentType, ctx)

if content == clients.ContentTypeCBOR {
return clients.PostRequest(ctx, "", event.CBOR(), e.urlClient)
} else {
return clients.PostJsonRequest("", event, ctx, e.urlClient)
return clients.PostJsonRequest(ctx, "", event, e.urlClient)
}
}

func (e *eventRestClient) AddBytes(event []byte, ctx context.Context) (string, error) {
func (e *eventRestClient) AddBytes(ctx context.Context, event []byte) (string, error) {
return clients.PostRequest(ctx, "", event, e.urlClient)
}

func (e *eventRestClient) Delete(id string, ctx context.Context) error {
func (e *eventRestClient) Delete(ctx context.Context, id string) error {
return clients.DeleteRequest(ctx, "/id/"+id, e.urlClient)
}

func (e *eventRestClient) DeleteForDevice(deviceId string, ctx context.Context) error {
func (e *eventRestClient) DeleteForDevice(ctx context.Context, id string) error {
return clients.DeleteRequest(ctx, "/device/"+url.QueryEscape(deviceId), e.urlClient)
}

func (e *eventRestClient) DeleteOld(age int, ctx context.Context) error {
func (e *eventRestClient) DeleteOld(ctx context.Context, age int) error {
return clients.DeleteRequest(ctx, "/removeold/age/"+strconv.Itoa(age), e.urlClient)
}

func (e *eventRestClient) MarkPushed(id string, ctx context.Context) error {
func (e *eventRestClient) MarkPushed(ctx context.Context, id string) error {
_, err := clients.PutRequest(ctx, "/id/"+id, nil, e.urlClient)
return err
}

func (e *eventRestClient) MarkPushedByChecksum(checksum string, ctx context.Context) error {
func (e *eventRestClient) MarkPushedByChecksum(ctx context.Context, checksum string) error {
_, err := clients.PutRequest(ctx, "/checksum/"+checksum, nil, e.urlClient)
return err
}
Expand Down
4 changes: 2 additions & 2 deletions clients/coredata/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestMarkPushed(t *testing.T) {

ec := NewEventClient(urlclient.NewLocalClient(ts.URL + clients.ApiEventRoute))

err := ec.MarkPushed(TestId, context.Background())
err := ec.MarkPushed(context.Background(), TestId)

if err != nil {
t.FailNow()
Expand All @@ -84,7 +84,7 @@ func TestMarkPushedByChecksum(t *testing.T) {

ec := NewEventClient(urlclient.NewLocalClient(ts.URL + clients.ApiEventRoute))

err := ec.MarkPushedByChecksum(TestChecksum, context.Background())
err := ec.MarkPushedByChecksum(context.Background(), TestChecksum)

if err != nil {
t.FailNow()
Expand Down
14 changes: 5 additions & 9 deletions clients/coredata/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ type ReadingClient interface {
// ReadingsForType returns readings up to a specified limit of a given type
ReadingsForType(ctx context.Context, readingType string, limit int) ([]models.Reading, error)
// ReadingsForInterval returns readings up to a specified limit generated within a specific time period
ReadingsForInterval(start int, end int, limit int, ctx context.Context) ([]models.Reading, error)
ReadingsForInterval(ctx context.Context, start int, end int, limit int) ([]models.Reading, error)
// Add a new reading
Add(readiing *models.Reading, ctx context.Context) (string, error)
Add(ctx context.Context, reading *models.Reading) (string, error)
// Delete eliminates a reading by its id
Delete(ctx context.Context, id string) error
}
Expand Down Expand Up @@ -148,17 +148,13 @@ func (r *readingRestClient) ReadingsForType(
return r.requestReadingSlice(ctx, "/type/"+url.QueryEscape(readingType)+"/"+strconv.Itoa(limit))
}

func (r *readingRestClient) ReadingsForInterval(
start int,
end int,
limit int,
ctx context.Context) ([]models.Reading, error) {
func (r *readingRestClient) ReadingsForInterval(ctx context.Context, start int, end int, limit int) ([]models.Reading, error) {

return r.requestReadingSlice(ctx, "/"+strconv.Itoa(start)+"/"+strconv.Itoa(end)+"/"+strconv.Itoa(limit))
}

func (r *readingRestClient) Add(reading *models.Reading, ctx context.Context) (string, error) {
return clients.PostJsonRequest("", reading, ctx, r.urlClient)
func (r *readingRestClient) Add(ctx context.Context, reading *models.Reading) (string, error) {
return clients.PostJsonRequest(ctx, "", reading, r.urlClient)
}

func (r *readingRestClient) Delete(ctx context.Context, id string) error {
Expand Down
78 changes: 34 additions & 44 deletions clients/coredata/value_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ type ValueDescriptorClient interface {
// ValueDescriptors returns a list of all value descriptors
ValueDescriptors(ctx context.Context) ([]models.ValueDescriptor, error)
// ValueDescriptor returns the value descriptor for the specified id
ValueDescriptor(id string, ctx context.Context) (models.ValueDescriptor, error)
ValueDescriptor(ctx context.Context, id string) (models.ValueDescriptor, error)
// ValueDescriptorForName returns the value descriptor for the specified name
ValueDescriptorForName(name string, ctx context.Context) (models.ValueDescriptor, error)
ValueDescriptorForName(ctx context.Context, name string) (models.ValueDescriptor, error)
// ValueDescriptorsByLabel returns the value descriptors for the specified label
ValueDescriptorsByLabel(label string, ctx context.Context) ([]models.ValueDescriptor, error)
ValueDescriptorsByLabel(ctx context.Context, label string) ([]models.ValueDescriptor, error)
// ValueDescriptorsForDevice returns the value descriptors associated with readings from the specified device (by id)
ValueDescriptorsForDevice(deviceId string, ctx context.Context) ([]models.ValueDescriptor, error)
ValueDescriptorsForDevice(ctx context.Context, deviceId string) ([]models.ValueDescriptor, error)
// ValueDescriptorsForDeviceByName returns the value descriptors associated with readings from the specified device (by name)
ValueDescriptorsForDeviceByName(deviceName string, ctx context.Context) ([]models.ValueDescriptor, error)
ValueDescriptorsForDeviceByName(ctx context.Context, deviceName string) ([]models.ValueDescriptor, error)
// ValueDescriptorsByUomLabel returns the value descriptors for the specified uomLabel
ValueDescriptorsByUomLabel(uomLabel string, ctx context.Context) ([]models.ValueDescriptor, error)
ValueDescriptorsByUomLabel(ctx context.Context, uomLabel string) ([]models.ValueDescriptor, error)
// ValueDescriptorsUsage return a map describing which ValueDescriptors are currently in use. The key is the
// ValueDescriptor name and the value is a bool specifying if the ValueDescriptor is in use.
ValueDescriptorsUsage(names []string, ctx context.Context) (map[string]bool, error)
ValueDescriptorsUsage(ctx context.Context, names []string) (map[string]bool, error)
// Adds the specified value descriptor
Add(vdr *models.ValueDescriptor, ctx context.Context) (string, error)
Add(ctx context.Context, vdr *models.ValueDescriptor) (string, error)
// Updates the specified value descriptor
Update(vdr *models.ValueDescriptor, ctx context.Context) error
Update(ctx context.Context, vdr *models.ValueDescriptor) error
// Delete eliminates a value descriptor (specified by id)
Delete(id string, ctx context.Context) error
Delete(ctx context.Context, id string) error
// Delete eliminates a value descriptor (specified by name)
DeleteByName(name string, ctx context.Context) error
DeleteByName(ctx context.Context, name string) error
}

type valueDescriptorRestClient struct {
Expand All @@ -66,8 +66,8 @@ func NewValueDescriptorClient(urlClient interfaces.URLClient) ValueDescriptorCli

// Helper method to request and decode a valuedescriptor slice
func (v *valueDescriptorRestClient) requestValueDescriptorSlice(
urlSuffix string,
ctx context.Context) ([]models.ValueDescriptor, error) {
ctx context.Context,
urlSuffix string) ([]models.ValueDescriptor, error) {

data, err := clients.GetRequest(ctx, urlSuffix, v.urlClient)
if err != nil {
Expand All @@ -81,8 +81,8 @@ func (v *valueDescriptorRestClient) requestValueDescriptorSlice(

// Helper method to request and decode a device
func (v *valueDescriptorRestClient) requestValueDescriptor(
urlSuffix string,
ctx context.Context) (models.ValueDescriptor, error) {
ctx context.Context,
urlSuffix string) (models.ValueDescriptor, error) {

data, err := clients.GetRequest(ctx, urlSuffix, v.urlClient)
if err != nil {
Expand All @@ -95,49 +95,39 @@ func (v *valueDescriptorRestClient) requestValueDescriptor(
}

func (v *valueDescriptorRestClient) ValueDescriptors(ctx context.Context) ([]models.ValueDescriptor, error) {
return v.requestValueDescriptorSlice("", ctx)
return v.requestValueDescriptorSlice(ctx, "")
}

func (v *valueDescriptorRestClient) ValueDescriptor(id string, ctx context.Context) (models.ValueDescriptor, error) {
return v.requestValueDescriptor("/"+id, ctx)
func (v *valueDescriptorRestClient) ValueDescriptor(ctx context.Context, id string) (models.ValueDescriptor, error) {
return v.requestValueDescriptor(ctx, "/"+id)
}

func (v *valueDescriptorRestClient) ValueDescriptorForName(
name string,
ctx context.Context) (models.ValueDescriptor, error) {
func (v *valueDescriptorRestClient) ValueDescriptorForName(ctx context.Context, name string) (models.ValueDescriptor, error) {

return v.requestValueDescriptor("/name/"+url.QueryEscape(name), ctx)
return v.requestValueDescriptor(ctx, "/name/"+url.QueryEscape(name))
}

func (v *valueDescriptorRestClient) ValueDescriptorsByLabel(
label string,
ctx context.Context) ([]models.ValueDescriptor, error) {
func (v *valueDescriptorRestClient) ValueDescriptorsByLabel(ctx context.Context, label string) ([]models.ValueDescriptor, error) {

return v.requestValueDescriptorSlice("/label/"+url.QueryEscape(label), ctx)
return v.requestValueDescriptorSlice(ctx, "/label/"+url.QueryEscape(label))
}

func (v *valueDescriptorRestClient) ValueDescriptorsForDevice(
deviceId string,
ctx context.Context) ([]models.ValueDescriptor, error) {
func (v *valueDescriptorRestClient) ValueDescriptorsForDevice(ctx context.Context, deviceId string) ([]models.ValueDescriptor, error) {

return v.requestValueDescriptorSlice("/deviceid/"+deviceId, ctx)
return v.requestValueDescriptorSlice(ctx, "/deviceid/"+deviceId)
}

func (v *valueDescriptorRestClient) ValueDescriptorsForDeviceByName(
deviceName string,
ctx context.Context) ([]models.ValueDescriptor, error) {
func (v *valueDescriptorRestClient) ValueDescriptorsForDeviceByName(ctx context.Context, deviceName string) ([]models.ValueDescriptor, error) {

return v.requestValueDescriptorSlice("/devicename/"+deviceName, ctx)
return v.requestValueDescriptorSlice(ctx, "/devicename/"+deviceName)
}

func (v *valueDescriptorRestClient) ValueDescriptorsByUomLabel(
uomLabel string,
ctx context.Context) ([]models.ValueDescriptor, error) {
func (v *valueDescriptorRestClient) ValueDescriptorsByUomLabel(ctx context.Context, uomLabel string) ([]models.ValueDescriptor, error) {

return v.requestValueDescriptorSlice("/uomlabel/"+uomLabel, ctx)
return v.requestValueDescriptorSlice(ctx, "/uomlabel/"+uomLabel)
}

func (v *valueDescriptorRestClient) ValueDescriptorsUsage(names []string, ctx context.Context) (map[string]bool, error) {
func (v *valueDescriptorRestClient) ValueDescriptorsUsage(ctx context.Context, names []string) (map[string]bool, error) {
urlPrefix, err := v.urlClient.Prefix()
if err != nil {
return nil, err
Expand Down Expand Up @@ -165,19 +155,19 @@ func (v *valueDescriptorRestClient) ValueDescriptorsUsage(names []string, ctx co
return usage, err
}

func (v *valueDescriptorRestClient) Add(vdr *models.ValueDescriptor, ctx context.Context) (string, error) {
return clients.PostJsonRequest("", vdr, ctx, v.urlClient)
func (v *valueDescriptorRestClient) Add(ctx context.Context, vdr *models.ValueDescriptor) (string, error) {
return clients.PostJsonRequest(ctx, "", vdr, v.urlClient)
}

func (v *valueDescriptorRestClient) Update(vdr *models.ValueDescriptor, ctx context.Context) error {
func (v *valueDescriptorRestClient) Update(ctx context.Context, vdr *models.ValueDescriptor) error {
return clients.UpdateRequest(ctx, "", vdr, v.urlClient)
}

func (v *valueDescriptorRestClient) Delete(id string, ctx context.Context) error {
func (v *valueDescriptorRestClient) Delete(ctx context.Context, id string) error {
return clients.DeleteRequest(ctx, "/id/"+id, v.urlClient)
}

func (v *valueDescriptorRestClient) DeleteByName(name string, ctx context.Context) error {
func (v *valueDescriptorRestClient) DeleteByName(ctx context.Context, name string) error {
return clients.DeleteRequest(ctx, "/name/"+name, v.urlClient)
}

Expand Down
Loading

0 comments on commit 28e9f23

Please sign in to comment.