Skip to content

Commit

Permalink
Merge branch 'master' into issue-551
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudxxx8 authored Mar 19, 2021
2 parents 1290a85 + d27e56e commit 36057ff
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 20 deletions.
3 changes: 2 additions & 1 deletion v2/clients/http/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/clients/http/utils"
Expand All @@ -28,7 +29,7 @@ func NewDeviceClient(baseUrl string) interfaces.DeviceClient {
}

func (dc DeviceClient) Add(ctx context.Context, reqs []requests.AddDeviceRequest) (res []common.BaseWithIdResponse, err errors.EdgeX) {
err = utils.PostRequest(ctx, &res, dc.baseUrl+v2.ApiDeviceRoute, reqs)
err = utils.PostRequest(ctx, &res, dc.baseUrl+v2.ApiDeviceRoute, reqs, clients.ContentTypeJSON)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
Expand Down
3 changes: 2 additions & 1 deletion v2/clients/http/deviceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"
"strings"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/clients/http/utils"
Expand All @@ -34,7 +35,7 @@ func NewDeviceProfileClient(baseUrl string) interfaces.DeviceProfileClient {

func (client *DeviceProfileClient) Add(ctx context.Context, reqs []requests.DeviceProfileRequest) ([]common.BaseWithIdResponse, errors.EdgeX) {
var responses []common.BaseWithIdResponse
err := utils.PostRequest(ctx, &responses, client.baseUrl+v2.ApiDeviceProfileRoute, reqs)
err := utils.PostRequest(ctx, &responses, client.baseUrl+v2.ApiDeviceProfileRoute, reqs, clients.ContentTypeJSON)
if err != nil {
return responses, errors.NewCommonEdgeXWrapper(err)
}
Expand Down
3 changes: 2 additions & 1 deletion v2/clients/http/deviceservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/clients/http/utils"
Expand All @@ -29,7 +30,7 @@ func NewDeviceServiceClient(baseUrl string) interfaces.DeviceServiceClient {

func (dsc DeviceServiceClient) Add(ctx context.Context, reqs []requests.AddDeviceServiceRequest) (
res []common.BaseWithIdResponse, err errors.EdgeX) {
err = utils.PostRequest(ctx, &res, dsc.baseUrl+v2.ApiDeviceServiceRoute, reqs)
err = utils.PostRequest(ctx, &res, dsc.baseUrl+v2.ApiDeviceServiceRoute, reqs, clients.ContentTypeJSON)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
Expand Down
5 changes: 3 additions & 2 deletions v2/clients/http/deviceservicecallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"path"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/clients/http/utils"
Expand All @@ -30,7 +31,7 @@ func NewDeviceServiceCallbackClient(baseUrl string) interfaces.DeviceServiceCall

func (client *deviceServiceCallbackClient) AddDeviceCallback(ctx context.Context, request requests.AddDeviceRequest) (common.BaseResponse, errors.EdgeX) {
var response common.BaseResponse
err := utils.PostRequest(ctx, &response, client.baseUrl+v2.ApiDeviceCallbackRoute, request)
err := utils.PostRequest(ctx, &response, client.baseUrl+v2.ApiDeviceCallbackRoute, request, clients.ContentTypeJSON)
if err != nil {
return response, errors.NewCommonEdgeXWrapper(err)
}
Expand Down Expand Up @@ -67,7 +68,7 @@ func (client *deviceServiceCallbackClient) UpdateDeviceProfileCallback(ctx conte

func (client *deviceServiceCallbackClient) AddProvisionWatcherCallback(ctx context.Context, request requests.AddProvisionWatcherRequest) (common.BaseResponse, errors.EdgeX) {
var response common.BaseResponse
err := utils.PostRequest(ctx, &response, client.baseUrl+v2.ApiWatcherCallbackRoute, request)
err := utils.PostRequest(ctx, &response, client.baseUrl+v2.ApiWatcherCallbackRoute, request, clients.ContentTypeJSON)
if err != nil {
return response, errors.NewCommonEdgeXWrapper(err)
}
Expand Down
17 changes: 16 additions & 1 deletion v2/clients/http/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ package http
import (
"context"
"net/url"
"os"
"path"
"strconv"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/clients/http/utils"
Expand All @@ -35,7 +37,20 @@ func (ec *eventClient) Add(ctx context.Context, req requests.AddEventRequest) (
common.BaseWithIdResponse, errors.EdgeX) {
path := path.Join(v2.ApiEventRoute, url.QueryEscape(req.Event.ProfileName), url.QueryEscape(req.Event.DeviceName), url.QueryEscape(req.Event.SourceName))
var br common.BaseWithIdResponse
err := utils.PostRequest(ctx, &br, ec.baseUrl+path, req)
var encoding string

for _, r := range req.Event.Readings {
if r.ValueType == v2.ValueTypeBinary {
encoding = clients.ContentTypeCBOR
break
}
}

if v := os.Getenv(v2.EnvEncodeAllEvents); v == v2.ValueTrue {
encoding = clients.ContentTypeCBOR
}

err := utils.PostRequest(ctx, &br, ec.baseUrl+path, req, encoding)
if err != nil {
return br, errors.NewCommonEdgeXWrapper(err)
}
Expand Down
3 changes: 2 additions & 1 deletion v2/clients/http/provisionwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"
"strings"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/clients/http/utils"
Expand All @@ -33,7 +34,7 @@ func NewProvisionWatcherClient(baseUrl string) interfaces.ProvisionWatcherClient
}

func (pwc ProvisionWatcherClient) Add(ctx context.Context, reqs []requests.AddProvisionWatcherRequest) (res []common.BaseWithIdResponse, err errors.EdgeX) {
err = utils.PostRequest(ctx, &res, pwc.baseUrl+v2.ApiProvisionWatcherRoute, reqs)
err = utils.PostRequest(ctx, &res, pwc.baseUrl+v2.ApiProvisionWatcherRoute, reqs, clients.ContentTypeJSON)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
Expand Down
27 changes: 20 additions & 7 deletions v2/clients/http/utils/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2020 IOTech Ltd
// Copyright (C) 2020-2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -17,6 +17,8 @@ import (
"net/url"
"path/filepath"

"github.com/fxamacker/cbor/v2"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"

Expand Down Expand Up @@ -79,18 +81,29 @@ func createRequest(ctx context.Context, httpMethod string, baseUrl string, reque
return req, nil
}

func createRequestWithRawData(ctx context.Context, httpMethod string, url string, data interface{}) (*http.Request, errors.EdgeX) {
jsonEncodedData, err := json.Marshal(data)
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to encode input data to JSON", err)
func createRequestWithRawData(ctx context.Context, httpMethod string, url string, data interface{}, encoding string) (*http.Request, errors.EdgeX) {
var err error
var encodedData []byte

if encoding == clients.ContentTypeJSON || encoding == "" {
encoding = clients.ContentTypeJSON
encodedData, err = json.Marshal(data)
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to encode input data to JSON", err)
}
} else if encoding == clients.ContentTypeCBOR {
encodedData, err = cbor.Marshal(data)
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindContractInvalid, "failed to encode input data to CBOR", err)
}
}

content := FromContext(ctx, clients.ContentType)
if content == "" {
content = clients.ContentTypeJSON
content = encoding
}

req, err := http.NewRequest(httpMethod, url, bytes.NewReader(jsonEncodedData))
req, err := http.NewRequest(httpMethod, url, bytes.NewReader(encodedData))
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindClientError, "failed to create a http request", err)
}
Expand Down
14 changes: 8 additions & 6 deletions v2/clients/http/utils/request.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2020 IOTech Ltd
// Copyright (C) 2020-2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -11,6 +11,7 @@ import (
"net/http"
"net/url"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
)

Expand All @@ -31,14 +32,15 @@ func GetRequest(ctx context.Context, returnValuePointer interface{}, baseUrl str
return nil
}

// Helper method to make the post JSON request and return the body
// Helper method to make the post JSON/CBOR request and return the body
func PostRequest(
ctx context.Context,
returnValuePointer interface{},
url string,
data interface{}) errors.EdgeX {
data interface{},
encoding string) errors.EdgeX {

req, err := createRequestWithRawData(ctx, http.MethodPost, url, data)
req, err := createRequestWithRawData(ctx, http.MethodPost, url, data, encoding)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
Expand All @@ -60,7 +62,7 @@ func PutRequest(
url string,
data interface{}) errors.EdgeX {

req, err := createRequestWithRawData(ctx, http.MethodPut, url, data)
req, err := createRequestWithRawData(ctx, http.MethodPut, url, data, clients.ContentTypeJSON)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
Expand All @@ -82,7 +84,7 @@ func PatchRequest(
url string,
data interface{}) errors.EdgeX {

req, err := createRequestWithRawData(ctx, http.MethodPatch, url, data)
req, err := createRequestWithRawData(ctx, http.MethodPatch, url, data, clients.ContentTypeJSON)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
Expand Down
7 changes: 7 additions & 0 deletions v2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ const (
CommaSeparator = ","
ValueYes = "yes"
ValueNo = "no"
ValueTrue = "true"
ValueFalse = "false"
)

// Constants related to Reading ValueTypes
Expand Down Expand Up @@ -205,3 +207,8 @@ const (
ReadWrite_W = "W"
ReadWrite_RW = "RW"
)

// Constants for Edgex Environment variable
const (
EnvEncodeAllEvents = "EDGEX_ENCODE_ALL_EVENTS_CBOR"
)

0 comments on commit 36057ff

Please sign in to comment.