From 28e9f23e8568d8b90eeab32f01a42be77ddef0f6 Mon Sep 17 00:00:00 2001 From: Brandon Forster Date: Sat, 1 Feb 2020 12:17:45 -0600 Subject: [PATCH] #206: Refactored so context is the first parameter in every client it's used in. Signed-off-by: Brandon Forster --- clients/coredata/event.go | 79 ++++++++++------------ clients/coredata/event_test.go | 4 +- clients/coredata/reading.go | 14 ++-- clients/coredata/value_descriptor.go | 78 ++++++++++----------- clients/coredata/value_descriptor_test.go | 6 +- clients/metadata/addressable.go | 28 ++++---- clients/metadata/addressable_test.go | 10 +-- clients/metadata/command.go | 38 +++++------ clients/metadata/device.go | 2 +- clients/metadata/device_profile.go | 48 ++++++------- clients/metadata/device_profile_test.go | 2 +- clients/metadata/device_service.go | 20 +++--- clients/metadata/provision_watcher.go | 60 ++++++++-------- clients/metadata/provision_watcher_test.go | 2 +- clients/notifications/client.go | 6 +- clients/notifications/client_test.go | 2 +- clients/request.go | 5 +- clients/scheduler/interval.go | 2 +- clients/scheduler/interval_action.go | 2 +- 19 files changed, 194 insertions(+), 214 deletions(-) diff --git a/clients/coredata/event.go b/clients/coredata/event.go index 952a3937..a29675f9 100644 --- a/clients/coredata/event.go +++ b/clients/coredata/event.go @@ -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) @@ -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 @@ -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 @@ -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 } diff --git a/clients/coredata/event_test.go b/clients/coredata/event_test.go index b76c1409..10688206 100644 --- a/clients/coredata/event_test.go +++ b/clients/coredata/event_test.go @@ -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() @@ -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() diff --git a/clients/coredata/reading.go b/clients/coredata/reading.go index 082e9599..f685f5af 100644 --- a/clients/coredata/reading.go +++ b/clients/coredata/reading.go @@ -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 } @@ -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 { diff --git a/clients/coredata/value_descriptor.go b/clients/coredata/value_descriptor.go index 679ca9c0..ea82198b 100644 --- a/clients/coredata/value_descriptor.go +++ b/clients/coredata/value_descriptor.go @@ -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 { @@ -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 { @@ -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 { @@ -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 @@ -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) } diff --git a/clients/coredata/value_descriptor_test.go b/clients/coredata/value_descriptor_test.go index 70888cfa..daed5ff3 100644 --- a/clients/coredata/value_descriptor_test.go +++ b/clients/coredata/value_descriptor_test.go @@ -116,7 +116,7 @@ func TestValueDescriptorUsage(t *testing.T) { defer ts.Close() vdc := NewValueDescriptorClient(urlclient.NewLocalClient(ts.URL + clients.ApiValueDescriptorRoute)) - usage, err := vdc.ValueDescriptorsUsage([]string{testValueDesciptorDescription1, testValueDesciptorDescription2}, context.Background()) + usage, err := vdc.ValueDescriptorsUsage(context.Background(), []string{testValueDesciptorDescription1, testValueDesciptorDescription2}) if err != nil { t.Errorf(err.Error()) t.FailNow() @@ -134,7 +134,7 @@ func TestValueDescriptorUsageSerializationError(t *testing.T) { defer ts.Close() vdc := NewValueDescriptorClient(urlclient.NewLocalClient(ts.URL + clients.ApiValueDescriptorRoute)) - _, err := vdc.ValueDescriptorsUsage([]string{testValueDesciptorDescription1, testValueDesciptorDescription2}, context.Background()) + _, err := vdc.ValueDescriptorsUsage(context.Background(), []string{testValueDesciptorDescription1, testValueDesciptorDescription2}) if err == nil { t.Error("Expected an error") return @@ -143,7 +143,7 @@ func TestValueDescriptorUsageSerializationError(t *testing.T) { func TestValueDescriptorUsageGetRequestError(t *testing.T) { vdc := NewValueDescriptorClient(urlclient.NewLocalClient("!%&")) - _, err := vdc.ValueDescriptorsUsage([]string{testValueDesciptorDescription1, testValueDesciptorDescription2}, context.Background()) + _, err := vdc.ValueDescriptorsUsage(context.Background(), []string{testValueDesciptorDescription1, testValueDesciptorDescription2}) if err == nil { t.Error("Expected an error") return diff --git a/clients/metadata/addressable.go b/clients/metadata/addressable.go index d8197bd0..b1add3ce 100644 --- a/clients/metadata/addressable.go +++ b/clients/metadata/addressable.go @@ -28,15 +28,15 @@ import ( // 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) + Add(ctx context.Context, addr *models.Addressable) (string, error) // Addressable returns an Addressable for the specified ID - Addressable(id string, ctx context.Context) (models.Addressable, error) + Addressable(ctx context.Context, id string) (models.Addressable, error) // Addressable returns an Addressable for the specified name - AddressableForName(name string, ctx context.Context) (models.Addressable, error) + AddressableForName(ctx context.Context, name string) (models.Addressable, error) // Update will update the Addressable data - Update(addr models.Addressable, ctx context.Context) error + Update(ctx context.Context, addr models.Addressable) error // Delete will eliminate the Addressable for the specified ID - Delete(id string, ctx context.Context) error + Delete(ctx context.Context, id string) error } type addressableRestClient struct { @@ -51,7 +51,7 @@ func NewAddressableClient(urlClient interfaces.URLClient) AddressableClient { } // Helper method to request and decode an addressable -func (a *addressableRestClient) requestAddressable(urlSuffix string, ctx context.Context) (models.Addressable, error) { +func (a *addressableRestClient) requestAddressable(ctx context.Context, urlSuffix string) (models.Addressable, error) { data, err := clients.GetRequest(ctx, urlSuffix, a.urlClient) if err != nil { return models.Addressable{}, err @@ -62,22 +62,22 @@ func (a *addressableRestClient) requestAddressable(urlSuffix string, ctx context return add, err } -func (a *addressableRestClient) Add(addr *models.Addressable, ctx context.Context) (string, error) { - return clients.PostJsonRequest("", addr, ctx, a.urlClient) +func (a *addressableRestClient) Add(ctx context.Context, addr *models.Addressable) (string, error) { + return clients.PostJsonRequest(ctx, "", addr, a.urlClient) } -func (a *addressableRestClient) Addressable(id string, ctx context.Context) (models.Addressable, error) { - return a.requestAddressable("/"+id, ctx) +func (a *addressableRestClient) Addressable(ctx context.Context, id string) (models.Addressable, error) { + return a.requestAddressable(ctx, "/"+id) } -func (a *addressableRestClient) AddressableForName(name string, ctx context.Context) (models.Addressable, error) { - return a.requestAddressable("/name/"+url.QueryEscape(name), ctx) +func (a *addressableRestClient) AddressableForName(ctx context.Context, name string) (models.Addressable, error) { + return a.requestAddressable(ctx, "/name/"+url.QueryEscape(name)) } -func (a *addressableRestClient) Update(addr models.Addressable, ctx context.Context) error { +func (a *addressableRestClient) Update(ctx context.Context, addr models.Addressable) error { return clients.UpdateRequest(ctx, "", addr, a.urlClient) } -func (a *addressableRestClient) Delete(id string, ctx context.Context) error { +func (a *addressableRestClient) Delete(ctx context.Context, id string) error { return clients.DeleteRequest(ctx, "/id/"+id, a.urlClient) } diff --git a/clients/metadata/addressable_test.go b/clients/metadata/addressable_test.go index 94110a55..487a91a9 100644 --- a/clients/metadata/addressable_test.go +++ b/clients/metadata/addressable_test.go @@ -75,7 +75,7 @@ func TestAddAddressable(t *testing.T) { ac := NewAddressableClient(urlclient.NewLocalClient(ts.URL + clients.ApiAddressableRoute)) - receivedAddressableID, err := ac.Add(&addressable, context.Background()) + receivedAddressableID, err := ac.Add(context.Background(), &addressable) if err != nil { t.Error(err.Error()) } @@ -112,7 +112,7 @@ func TestGetAddressable(t *testing.T) { ac := NewAddressableClient(urlclient.NewLocalClient(ts.URL + clients.ApiAddressableRoute)) - receivedAddressable, err := ac.Addressable(addressable.Id, context.Background()) + receivedAddressable, err := ac.Addressable(context.Background(), addressable.Id) if err != nil { t.Fatal(err.Error()) } @@ -149,7 +149,7 @@ func TestGetAddressableForName(t *testing.T) { ac := NewAddressableClient(urlclient.NewLocalClient(ts.URL + clients.ApiAddressableRoute)) - receivedAddressable, err := ac.AddressableForName(addressable.Name, context.Background()) + receivedAddressable, err := ac.AddressableForName(context.Background(), addressable.Name) if err != nil { t.Fatal(err.Error()) } @@ -183,7 +183,7 @@ func TestUpdateAddressable(t *testing.T) { ac := NewAddressableClient(urlclient.NewLocalClient(ts.URL + clients.ApiAddressableRoute)) - err := ac.Update(addressable, context.Background()) + err := ac.Update(context.Background(), addressable) if err != nil { t.Error(err.Error()) } @@ -214,7 +214,7 @@ func TestDeleteAddressable(t *testing.T) { ac := NewAddressableClient(urlclient.NewLocalClient(ts.URL + clients.ApiAddressableRoute)) - err := ac.Delete(addressable.Id, context.Background()) + err := ac.Delete(context.Background(), addressable.Id) if err != nil { t.Error(err.Error()) } diff --git a/clients/metadata/command.go b/clients/metadata/command.go index 5fab975c..c4cbafcf 100644 --- a/clients/metadata/command.go +++ b/clients/metadata/command.go @@ -26,19 +26,19 @@ import ( // CommandClient defines the interface for interactions with the Command endpoint on core-metadata. type CommandClient interface { // Add a new command - Add(com *models.Command, ctx context.Context) (string, error) + Add(ctx context.Context, com *models.Command) (string, error) // Command obtains the command for the specified ID - Command(id string, ctx context.Context) (models.Command, error) + Command(ctx context.Context, id string) (models.Command, error) // Commands lists all the commands Commands(ctx context.Context) ([]models.Command, error) // CommandsForName lists all the commands for the specified name - CommandsForName(name string, ctx context.Context) ([]models.Command, error) + CommandsForName(ctx context.Context, name string) ([]models.Command, error) // CommandsForDeviceId list all commands for device with specified ID - CommandsForDeviceId(id string, ctx context.Context) ([]models.Command, error) + CommandsForDeviceId(ctx context.Context, id string) ([]models.Command, error) // Delete a command for the specified ID - Delete(id string, ctx context.Context) error + Delete(ctx context.Context, id string) error // Update a command - Update(com models.Command, ctx context.Context) error + Update(ctx context.Context, com models.Command) error } type commandRestClient struct { @@ -53,7 +53,7 @@ func NewCommandClient(urlClient interfaces.URLClient) CommandClient { } // Helper method to request and decode a command -func (c *commandRestClient) requestCommand(urlSuffix string, ctx context.Context) (models.Command, error) { +func (c *commandRestClient) requestCommand(ctx context.Context, urlSuffix string) (models.Command, error) { data, err := clients.GetRequest(ctx, urlSuffix, c.urlClient) if err != nil { return models.Command{}, err @@ -65,7 +65,7 @@ func (c *commandRestClient) requestCommand(urlSuffix string, ctx context.Context } // Helper method to request and decode a command slice -func (c *commandRestClient) requestCommandSlice(urlSuffix string, ctx context.Context) ([]models.Command, error) { +func (c *commandRestClient) requestCommandSlice(ctx context.Context, urlSuffix string) ([]models.Command, error) { data, err := clients.GetRequest(ctx, urlSuffix, c.urlClient) if err != nil { return []models.Command{}, err @@ -76,30 +76,30 @@ func (c *commandRestClient) requestCommandSlice(urlSuffix string, ctx context.Co return comSlice, err } -func (c *commandRestClient) Command(id string, ctx context.Context) (models.Command, error) { - return c.requestCommand("/"+id, ctx) +func (c *commandRestClient) Command(ctx context.Context, id string) (models.Command, error) { + return c.requestCommand(ctx, "/"+id) } func (c *commandRestClient) Commands(ctx context.Context) ([]models.Command, error) { - return c.requestCommandSlice("", ctx) + return c.requestCommandSlice(ctx, "") } -func (c *commandRestClient) CommandsForName(name string, ctx context.Context) ([]models.Command, error) { - return c.requestCommandSlice("/name/"+name, ctx) +func (c *commandRestClient) CommandsForName(ctx context.Context, name string) ([]models.Command, error) { + return c.requestCommandSlice(ctx, "/name/"+name) } -func (c *commandRestClient) CommandsForDeviceId(id string, ctx context.Context) ([]models.Command, error) { - return c.requestCommandSlice("/device/"+id, ctx) +func (c *commandRestClient) CommandsForDeviceId(ctx context.Context, id string) ([]models.Command, error) { + return c.requestCommandSlice(ctx, "/device/"+id) } -func (c *commandRestClient) Add(com *models.Command, ctx context.Context) (string, error) { - return clients.PostJsonRequest("", com, ctx, c.urlClient) +func (c *commandRestClient) Add(ctx context.Context, com *models.Command) (string, error) { + return clients.PostJsonRequest(ctx, "", com, c.urlClient) } -func (c *commandRestClient) Update(com models.Command, ctx context.Context) error { +func (c *commandRestClient) Update(ctx context.Context, com models.Command) error { return clients.UpdateRequest(ctx, "", com, c.urlClient) } -func (c *commandRestClient) Delete(id string, ctx context.Context) error { +func (c *commandRestClient) Delete(ctx context.Context, id string) error { return clients.DeleteRequest(ctx, "/id/"+id, c.urlClient) } diff --git a/clients/metadata/device.go b/clients/metadata/device.go index 4bb5e8cf..5fbcac62 100644 --- a/clients/metadata/device.go +++ b/clients/metadata/device.go @@ -143,7 +143,7 @@ func (d *deviceRestClient) DevicesForProfileByName(ctx context.Context, profileN } func (d *deviceRestClient) Add(ctx context.Context, dev *models.Device) (string, error) { - return clients.PostJsonRequest("", dev, ctx, d.urlClient) + return clients.PostJsonRequest(ctx, "", dev, d.urlClient) } func (d *deviceRestClient) Update(ctx context.Context, dev models.Device) error { diff --git a/clients/metadata/device_profile.go b/clients/metadata/device_profile.go index c78621fc..e892dec8 100644 --- a/clients/metadata/device_profile.go +++ b/clients/metadata/device_profile.go @@ -27,23 +27,23 @@ import ( // DeviceProfileClient defines the interface for interactions with the DeviceProfile endpoint on metadata. type DeviceProfileClient interface { // Add a new device profile - Add(dp *models.DeviceProfile, ctx context.Context) (string, error) + Add(ctx context.Context, dp *models.DeviceProfile) (string, error) // Delete eliminates a device profile for the specified ID - Delete(id string, ctx context.Context) error + Delete(ctx context.Context, id string) error // DeleteByName eliminates a device profile for the specified name - DeleteByName(name string, ctx context.Context) error + DeleteByName(ctx context.Context, name string) error // DeviceProfile loads the device profile for the specified ID - DeviceProfile(id string, ctx context.Context) (models.DeviceProfile, error) + DeviceProfile(ctx context.Context, id string) (models.DeviceProfile, error) // DeviceProfiles lists all device profiles DeviceProfiles(ctx context.Context) ([]models.DeviceProfile, error) // DeviceProfileForName loads the device profile for the specified name - DeviceProfileForName(name string, ctx context.Context) (models.DeviceProfile, error) + DeviceProfileForName(ctx context.Context, name string) (models.DeviceProfile, error) // Update a device profile - Update(dp models.DeviceProfile, ctx context.Context) error + Update(ctx context.Context, dp models.DeviceProfile) error // Upload a new device profile using raw YAML content - Upload(yamlString string, ctx context.Context) (string, error) + Upload(ctx context.Context, yamlString string) (string, error) // Upload a new device profile using a file in YAML format - UploadFile(yamlFilePath string, ctx context.Context) (string, error) + UploadFile(ctx context.Context, yamlFilePath string) (string, error) } type deviceProfileRestClient struct { @@ -59,8 +59,8 @@ func NewDeviceProfileClient(urlClient interfaces.URLClient) DeviceProfileClient // Helper method to request and decode a device profile func (dpc *deviceProfileRestClient) requestDeviceProfile( - urlSuffix string, - ctx context.Context) (models.DeviceProfile, error) { + ctx context.Context, + urlSuffix string) (models.DeviceProfile, error) { data, err := clients.GetRequest(ctx, urlSuffix, dpc.urlClient) if err != nil { @@ -74,8 +74,8 @@ func (dpc *deviceProfileRestClient) requestDeviceProfile( // Helper method to request and decode a device profile slice func (dpc *deviceProfileRestClient) requestDeviceProfileSlice( - urlSuffix string, - ctx context.Context) ([]models.DeviceProfile, error) { + ctx context.Context, + urlSuffix string) ([]models.DeviceProfile, error) { data, err := clients.GetRequest(ctx, urlSuffix, dpc.urlClient) if err != nil { @@ -87,40 +87,40 @@ func (dpc *deviceProfileRestClient) requestDeviceProfileSlice( return dpSlice, err } -func (dpc *deviceProfileRestClient) Add(dp *models.DeviceProfile, ctx context.Context) (string, error) { - return clients.PostJsonRequest("", dp, ctx, dpc.urlClient) +func (dpc *deviceProfileRestClient) Add(ctx context.Context, dp *models.DeviceProfile) (string, error) { + return clients.PostJsonRequest(ctx, "", dp, dpc.urlClient) } -func (dpc *deviceProfileRestClient) Delete(id string, ctx context.Context) error { +func (dpc *deviceProfileRestClient) Delete(ctx context.Context, id string) error { return clients.DeleteRequest(ctx, "/id/"+id, dpc.urlClient) } -func (dpc *deviceProfileRestClient) DeleteByName(name string, ctx context.Context) error { +func (dpc *deviceProfileRestClient) DeleteByName(ctx context.Context, name string) error { return clients.DeleteRequest(ctx, "/name/"+url.QueryEscape(name), dpc.urlClient) } -func (dpc *deviceProfileRestClient) DeviceProfile(id string, ctx context.Context) (models.DeviceProfile, error) { - return dpc.requestDeviceProfile("/"+id, ctx) +func (dpc *deviceProfileRestClient) DeviceProfile(ctx context.Context, id string) (models.DeviceProfile, error) { + return dpc.requestDeviceProfile(ctx, "/"+id) } func (dpc *deviceProfileRestClient) DeviceProfiles(ctx context.Context) ([]models.DeviceProfile, error) { - return dpc.requestDeviceProfileSlice("", ctx) + return dpc.requestDeviceProfileSlice(ctx, "") } -func (dpc *deviceProfileRestClient) DeviceProfileForName(name string, ctx context.Context) (models.DeviceProfile, error) { - return dpc.requestDeviceProfile("/name/"+name, ctx) +func (dpc *deviceProfileRestClient) DeviceProfileForName(ctx context.Context, name string) (models.DeviceProfile, error) { + return dpc.requestDeviceProfile(ctx, "/name/"+name) } -func (dpc *deviceProfileRestClient) Update(dp models.DeviceProfile, ctx context.Context) error { +func (dpc *deviceProfileRestClient) Update(ctx context.Context, dp models.DeviceProfile) error { return clients.UpdateRequest(ctx, "", dp, dpc.urlClient) } -func (dpc *deviceProfileRestClient) Upload(yamlString string, ctx context.Context) (string, error) { +func (dpc *deviceProfileRestClient) Upload(ctx context.Context, yamlString string) (string, error) { ctx = context.WithValue(ctx, clients.ContentType, clients.ContentTypeYAML) return clients.PostRequest(ctx, "/upload", []byte(yamlString), dpc.urlClient) } -func (dpc *deviceProfileRestClient) UploadFile(yamlFilePath string, ctx context.Context) (string, error) { +func (dpc *deviceProfileRestClient) UploadFile(ctx context.Context, yamlFilePath string) (string, error) { return clients.UploadFileRequest(ctx, "/uploadfile", yamlFilePath, dpc.urlClient) } diff --git a/clients/metadata/device_profile_test.go b/clients/metadata/device_profile_test.go index af7b1082..bda4d45c 100644 --- a/clients/metadata/device_profile_test.go +++ b/clients/metadata/device_profile_test.go @@ -70,7 +70,7 @@ func TestUpdateDeviceProfile(t *testing.T) { dpc := NewDeviceProfileClient(urlclient.NewLocalClient(ts.URL + clients.ApiDeviceProfileRoute)) - err := dpc.Update(p, context.Background()) + err := dpc.Update(context.Background(), p) if err != nil { t.Error(err.Error()) } diff --git a/clients/metadata/device_service.go b/clients/metadata/device_service.go index cd7726b9..2dcf99f3 100644 --- a/clients/metadata/device_service.go +++ b/clients/metadata/device_service.go @@ -27,13 +27,13 @@ import ( // DeviceServiceClient defines the interface for interactions with the DeviceService endpoint on metadata. type DeviceServiceClient interface { // Add a new device service - Add(ds *models.DeviceService, ctx context.Context) (string, error) + Add(ctx context.Context, ds *models.DeviceService) (string, error) // DeviceServiceForName loads a device service for the specified name - DeviceServiceForName(name string, ctx context.Context) (models.DeviceService, error) + DeviceServiceForName(ctx context.Context, name string) (models.DeviceService, error) // UpdateLastConnected updates a device service's last connected timestamp for the specified service ID - UpdateLastConnected(id string, time int64, ctx context.Context) error + UpdateLastConnected(ctx context.Context, id string, time int64) error // UpdateLastReported updates a device service's last reported timestamp for the specified service ID - UpdateLastReported(id string, time int64, ctx context.Context) error + UpdateLastReported(ctx context.Context, id string, time int64) error } type deviceServiceRestClient struct { @@ -47,23 +47,21 @@ func NewDeviceServiceClient(urlClient interfaces.URLClient) DeviceServiceClient } } -func (dsc *deviceServiceRestClient) UpdateLastConnected(id string, time int64, ctx context.Context) error { +func (dsc *deviceServiceRestClient) UpdateLastConnected(ctx context.Context, id string, time int64) error { _, err := clients.PutRequest(ctx, "/"+id+"/lastconnected/"+strconv.FormatInt(time, 10), nil, dsc.urlClient) return err } -func (dsc *deviceServiceRestClient) UpdateLastReported(id string, time int64, ctx context.Context) error { +func (dsc *deviceServiceRestClient) UpdateLastReported(ctx context.Context, id string, time int64) error { _, err := clients.PutRequest(ctx, "/"+id+"/lastreported/"+strconv.FormatInt(time, 10), nil, dsc.urlClient) return err } -func (dsc *deviceServiceRestClient) Add(ds *models.DeviceService, ctx context.Context) (string, error) { - return clients.PostJsonRequest("", ds, ctx, dsc.urlClient) +func (dsc *deviceServiceRestClient) Add(ctx context.Context, ds *models.DeviceService) (string, error) { + return clients.PostJsonRequest(ctx, "", ds, dsc.urlClient) } -func (dsc *deviceServiceRestClient) DeviceServiceForName( - name string, - ctx context.Context) (models.DeviceService, error) { +func (dsc *deviceServiceRestClient) DeviceServiceForName(ctx context.Context, name string) (models.DeviceService, error) { data, err := clients.GetRequest(ctx, "/name/"+name, dsc.urlClient) if err != nil { diff --git a/clients/metadata/provision_watcher.go b/clients/metadata/provision_watcher.go index 045d8482..7d77ea9e 100644 --- a/clients/metadata/provision_watcher.go +++ b/clients/metadata/provision_watcher.go @@ -27,25 +27,25 @@ import ( // ProvisionWatcherClient defines the interface for interactions with the ProvisionWatcher endpoint on metadata. type ProvisionWatcherClient interface { // Add a new provision watcher - Add(dev *models.ProvisionWatcher, ctx context.Context) (string, error) + Add(ctx context.Context, dev *models.ProvisionWatcher) (string, error) // Delete a provision watcher for the specified ID - Delete(id string, ctx context.Context) error + Delete(ctx context.Context, id string) error // ProvisionWatcher loads an instance of a provision watcher for the specified ID - ProvisionWatcher(id string, ctx context.Context) (models.ProvisionWatcher, error) + ProvisionWatcher(ctx context.Context, id string) (models.ProvisionWatcher, error) // ProvisionWatcherForName loads an instance of a provision watcher for the specified name - ProvisionWatcherForName(name string, ctx context.Context) (models.ProvisionWatcher, error) + ProvisionWatcherForName(ctx context.Context, name string) (models.ProvisionWatcher, error) // ProvisionWatchers lists all provision watchers. ProvisionWatchers(ctx context.Context) ([]models.ProvisionWatcher, error) // ProvisionWatchersForService lists all provision watchers associated with the specified device service id - ProvisionWatchersForService(serviceId string, ctx context.Context) ([]models.ProvisionWatcher, error) + ProvisionWatchersForService(ctx context.Context, serviceId string) ([]models.ProvisionWatcher, error) // ProvisionWatchersForServiceByName lists all provision watchers associated with the specified device service name - ProvisionWatchersForServiceByName(serviceName string, ctx context.Context) ([]models.ProvisionWatcher, error) + ProvisionWatchersForServiceByName(ctx context.Context, serviceName string) ([]models.ProvisionWatcher, error) // ProvisionWatchersForProfile lists all provision watchers associated with the specified device profile ID - ProvisionWatchersForProfile(profileid string, ctx context.Context) ([]models.ProvisionWatcher, error) + ProvisionWatchersForProfile(ctx context.Context, profileID string) ([]models.ProvisionWatcher, error) // ProvisionWatchersForProfileByName lists all provision watchers associated with the specified device profile name - ProvisionWatchersForProfileByName(profileName string, ctx context.Context) ([]models.ProvisionWatcher, error) + ProvisionWatchersForProfileByName(ctx context.Context, profileName string) ([]models.ProvisionWatcher, error) // Update the provision watcher - Update(dev models.ProvisionWatcher, ctx context.Context) error + Update(ctx context.Context, dev models.ProvisionWatcher) error } type provisionWatcherRestClient struct { @@ -61,8 +61,8 @@ func NewProvisionWatcherClient(urlClient interfaces.URLClient) ProvisionWatcherC // Helper method to request and decode a provision watcher func (pwc *provisionWatcherRestClient) requestProvisionWatcher( - urlSuffix string, - ctx context.Context) (models.ProvisionWatcher, error) { + ctx context.Context, + urlSuffix string) (models.ProvisionWatcher, error) { data, err := clients.GetRequest(ctx, urlSuffix, pwc.urlClient) if err != nil { @@ -76,8 +76,8 @@ func (pwc *provisionWatcherRestClient) requestProvisionWatcher( // Helper method to request and decode a provision watcher slice func (pwc *provisionWatcherRestClient) requestProvisionWatcherSlice( - urlSuffix string, - ctx context.Context) ([]models.ProvisionWatcher, error) { + ctx context.Context, + urlSuffix string) ([]models.ProvisionWatcher, error) { data, err := clients.GetRequest(ctx, urlSuffix, pwc.urlClient) if err != nil { @@ -89,42 +89,42 @@ func (pwc *provisionWatcherRestClient) requestProvisionWatcherSlice( return pwSlice, err } -func (pwc *provisionWatcherRestClient) ProvisionWatcher(id string, ctx context.Context) (models.ProvisionWatcher, error) { - return pwc.requestProvisionWatcher("/"+id, ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatcher(ctx context.Context, id string) (models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcher(ctx, "/"+id) } func (pwc *provisionWatcherRestClient) ProvisionWatchers(ctx context.Context) ([]models.ProvisionWatcher, error) { - return pwc.requestProvisionWatcherSlice("", ctx) + return pwc.requestProvisionWatcherSlice(ctx, "") } -func (pwc *provisionWatcherRestClient) ProvisionWatcherForName(name string, ctx context.Context) (models.ProvisionWatcher, error) { - return pwc.requestProvisionWatcher("/name/"+url.QueryEscape(name), ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatcherForName(ctx context.Context, name string) (models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcher(ctx, "/name/"+url.QueryEscape(name)) } -func (pwc *provisionWatcherRestClient) ProvisionWatchersForService(serviceId string, ctx context.Context) ([]models.ProvisionWatcher, error) { - return pwc.requestProvisionWatcherSlice("/service/"+serviceId, ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatchersForService(ctx context.Context, serviceId string) ([]models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcherSlice(ctx, "/service/"+serviceId) } -func (pwc *provisionWatcherRestClient) ProvisionWatchersForServiceByName(serviceName string, ctx context.Context) ([]models.ProvisionWatcher, error) { - return pwc.requestProvisionWatcherSlice("/servicename/"+url.QueryEscape(serviceName), ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatchersForServiceByName(ctx context.Context, serviceName string) ([]models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcherSlice(ctx, "/servicename/"+url.QueryEscape(serviceName)) } -func (pwc *provisionWatcherRestClient) ProvisionWatchersForProfile(profileId string, ctx context.Context) ([]models.ProvisionWatcher, error) { - return pwc.requestProvisionWatcherSlice("/profile/"+profileId, ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatchersForProfile(ctx context.Context, profileID string) ([]models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcherSlice(ctx, "/profile/"+profileID) } -func (pwc *provisionWatcherRestClient) ProvisionWatchersForProfileByName(profileName string, ctx context.Context) ([]models.ProvisionWatcher, error) { - return pwc.requestProvisionWatcherSlice("/profilename/"+url.QueryEscape(profileName), ctx) +func (pwc *provisionWatcherRestClient) ProvisionWatchersForProfileByName(ctx context.Context, profileName string) ([]models.ProvisionWatcher, error) { + return pwc.requestProvisionWatcherSlice(ctx, "/profilename/"+url.QueryEscape(profileName)) } -func (pwc *provisionWatcherRestClient) Add(dev *models.ProvisionWatcher, ctx context.Context) (string, error) { - return clients.PostJsonRequest("", dev, ctx, pwc.urlClient) +func (pwc *provisionWatcherRestClient) Add(ctx context.Context, dev *models.ProvisionWatcher) (string, error) { + return clients.PostJsonRequest(ctx, "", dev, pwc.urlClient) } -func (pwc *provisionWatcherRestClient) Update(dev models.ProvisionWatcher, ctx context.Context) error { +func (pwc *provisionWatcherRestClient) Update(ctx context.Context, dev models.ProvisionWatcher) error { return clients.UpdateRequest(ctx, "", dev, pwc.urlClient) } -func (pwc *provisionWatcherRestClient) Delete(id string, ctx context.Context) error { +func (pwc *provisionWatcherRestClient) Delete(ctx context.Context, id string) error { return clients.DeleteRequest(ctx, "/id/"+id, pwc.urlClient) } diff --git a/clients/metadata/provision_watcher_test.go b/clients/metadata/provision_watcher_test.go index c185ebb7..9440f9d3 100644 --- a/clients/metadata/provision_watcher_test.go +++ b/clients/metadata/provision_watcher_test.go @@ -57,7 +57,7 @@ func TestAddProvisionWatcher(t *testing.T) { sc := NewProvisionWatcherClient(urlclient.NewLocalClient(ts.URL + clients.ApiProvisionWatcherRoute)) - receivedProvisionWatcherID, err := sc.Add(&se, context.Background()) + receivedProvisionWatcherID, err := sc.Add(context.Background(), &se) if err != nil { t.Error(err.Error()) } diff --git a/clients/notifications/client.go b/clients/notifications/client.go index 4b8488f6..7d15c55a 100644 --- a/clients/notifications/client.go +++ b/clients/notifications/client.go @@ -48,7 +48,7 @@ const ( // 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 + SendNotification(ctx context.Context, n Notification) error } // Type struct for REST-specific implementation of the NotificationsClient interface @@ -78,7 +78,7 @@ func NewNotificationsClient(urlClient interfaces.URLClient) NotificationsClient } } -func (nc *notificationsRestClient) SendNotification(n Notification, ctx context.Context) error { - _, err := clients.PostJsonRequest("", n, ctx, nc.urlClient) +func (nc *notificationsRestClient) SendNotification(ctx context.Context, n Notification) error { + _, err := clients.PostJsonRequest(ctx, "", n, nc.urlClient) return err } diff --git a/clients/notifications/client_test.go b/clients/notifications/client_test.go index b9848cf1..e0a2afb5 100644 --- a/clients/notifications/client_test.go +++ b/clients/notifications/client_test.go @@ -105,5 +105,5 @@ func TestReceiveNotification(t *testing.T) { Labels: []string{TestNotificationLabel1, TestNotificationLabel2}, } - _ = nc.SendNotification(notification, context.Background()) + _ = nc.SendNotification(context.Background(), notification) } diff --git a/clients/request.go b/clients/request.go index 9907dafe..96b63314 100644 --- a/clients/request.go +++ b/clients/request.go @@ -103,9 +103,9 @@ func CountRequest(ctx context.Context, urlSuffix string, urlClient interfaces.UR // Helper method to make the post JSON request and return the body func PostJsonRequest( + ctx context.Context, urlSuffix string, data interface{}, - ctx context.Context, urlClient interfaces.URLClient) (string, error) { jsonStr, err := json.Marshal(data) @@ -262,6 +262,9 @@ func PutRequest(ctx context.Context, urlSuffix string, body []byte, urlClient in } if body != nil { req, err = http.NewRequest(http.MethodPut, urlPrefix+urlSuffix, bytes.NewReader(body)) + if err != nil { + return "", err + } content := FromContext(ContentType, ctx) if content == "" { diff --git a/clients/scheduler/interval.go b/clients/scheduler/interval.go index b8546de7..5a6aa260 100644 --- a/clients/scheduler/interval.go +++ b/clients/scheduler/interval.go @@ -55,7 +55,7 @@ func NewIntervalClient(urlClient interfaces.URLClient) IntervalClient { } func (ic *intervalRestClient) Add(interval *models.Interval, ctx context.Context) (string, error) { - return clients.PostJsonRequest("", interval, ctx, ic.urlClient) + return clients.PostJsonRequest(ctx, "", interval, ic.urlClient) } func (ic *intervalRestClient) Delete(id string, ctx context.Context) error { diff --git a/clients/scheduler/interval_action.go b/clients/scheduler/interval_action.go index 1a6a993e..80b049f8 100644 --- a/clients/scheduler/interval_action.go +++ b/clients/scheduler/interval_action.go @@ -94,7 +94,7 @@ func (iac *intervalActionRestClient) requestIntervalActionSlice( } func (iac *intervalActionRestClient) Add(ia *models.IntervalAction, ctx context.Context) (string, error) { - return clients.PostJsonRequest("", ia, ctx, iac.urlClient) + return clients.PostJsonRequest(ctx, "", ia, iac.urlClient) } func (iac *intervalActionRestClient) Delete(id string, ctx context.Context) error {