Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Rename Encode func to better name #3822

Merged
merged 2 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions internal/core/command/controller/http/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import (
"math"
"net/http"

"github.com/edgexfoundry/edgex-go/internal/core/command/application"
commandContainer "github.com/edgexfoundry/edgex-go/internal/core/command/container"
"github.com/edgexfoundry/edgex-go/internal/pkg"
"github.com/edgexfoundry/edgex-go/internal/pkg/utils"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
"github.com/edgexfoundry/go-mod-core-contracts/v2/common"
responseDTO "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/responses"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"

"github.com/edgexfoundry/edgex-go/internal/core/command/application"
commandContainer "github.com/edgexfoundry/edgex-go/internal/core/command/container"
"github.com/edgexfoundry/edgex-go/internal/pkg"
"github.com/edgexfoundry/edgex-go/internal/pkg/utils"

"github.com/gorilla/mux"
)

Expand Down Expand Up @@ -54,7 +55,7 @@ func (cc *CommandController) AllCommands(w http.ResponseWriter, r *http.Request)
response := responseDTO.NewMultiDeviceCoreCommandsResponse("", "", http.StatusOK, totalCount, commands)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
// encode and send out the response
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (cc *CommandController) CommandsByDeviceName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -74,7 +75,7 @@ func (cc *CommandController) CommandsByDeviceName(w http.ResponseWriter, r *http
response := responseDTO.NewDeviceCoreCommandResponse("", "", http.StatusOK, deviceCoreCommand)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
// encode and send out the response
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func validateGetCommandParameters(r *http.Request) (err errors.EdgeX) {
Expand Down Expand Up @@ -114,7 +115,7 @@ func (cc *CommandController) IssueGetCommandByName(w http.ResponseWriter, r *htt
// encode and send out the response
if response != nil {
utils.WriteHttpHeader(w, ctx, response.StatusCode)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
} else {
// If dsReturnEvent is no, there will be no content returned in the http response
utils.WriteHttpHeader(w, ctx, http.StatusOK)
Expand Down Expand Up @@ -147,5 +148,5 @@ func (cc *CommandController) IssueSetCommandByName(w http.ResponseWriter, r *htt

utils.WriteHttpHeader(w, ctx, response.StatusCode)
// encode and send out the response
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}
20 changes: 10 additions & 10 deletions internal/core/data/controller/http/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (ec *EventController) AddEvent(w http.ResponseWriter, r *http.Request) {
response := commonDTO.NewBaseWithIdResponse(addEventReqDTO.RequestId, "", http.StatusCreated, event.Id)
utils.WriteHttpHeader(w, ctx, http.StatusCreated)
// encode and send out the response
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (ec *EventController) EventById(w http.ResponseWriter, r *http.Request) {
Expand All @@ -122,7 +122,7 @@ func (ec *EventController) EventById(w http.ResponseWriter, r *http.Request) {
response := responseDTO.NewEventResponse("", "", http.StatusOK, e)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
// encode and send out the response
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (ec *EventController) DeleteEventById(w http.ResponseWriter, r *http.Request) {
Expand All @@ -145,7 +145,7 @@ func (ec *EventController) DeleteEventById(w http.ResponseWriter, r *http.Reques
response := commonDTO.NewBaseResponse("", "", http.StatusOK)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
// encode and send out the response
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (ec *EventController) EventTotalCount(w http.ResponseWriter, r *http.Request) {
Expand All @@ -162,7 +162,7 @@ func (ec *EventController) EventTotalCount(w http.ResponseWriter, r *http.Reques

response := commonDTO.NewCountResponse("", "", http.StatusOK, count)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc) // encode and send out the response
pkg.EncodeAndWriteResponse(response, w, lc) // encode and send out the response
}

func (ec *EventController) EventCountByDeviceName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -183,7 +183,7 @@ func (ec *EventController) EventCountByDeviceName(w http.ResponseWriter, r *http

response := commonDTO.NewCountResponse("", "", http.StatusOK, count)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc) // encode and send out the response
pkg.EncodeAndWriteResponse(response, w, lc) // encode and send out the response
}

func (ec *EventController) AllEvents(w http.ResponseWriter, r *http.Request) {
Expand All @@ -204,7 +204,7 @@ func (ec *EventController) AllEvents(w http.ResponseWriter, r *http.Request) {
}
response := responseDTO.NewMultiEventsResponse("", "", http.StatusOK, totalCount, events)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (ec *EventController) EventsByDeviceName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -229,7 +229,7 @@ func (ec *EventController) EventsByDeviceName(w http.ResponseWriter, r *http.Req

response := responseDTO.NewMultiEventsResponse("", "", http.StatusOK, totalCount, events)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (ec *EventController) DeleteEventsByDeviceName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -250,7 +250,7 @@ func (ec *EventController) DeleteEventsByDeviceName(w http.ResponseWriter, r *ht
response := commonDTO.NewBaseResponse("", "", http.StatusAccepted)
utils.WriteHttpHeader(w, ctx, http.StatusAccepted)
// encode and send out the response
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (ec *EventController) EventsByTimeRange(w http.ResponseWriter, r *http.Request) {
Expand All @@ -272,7 +272,7 @@ func (ec *EventController) EventsByTimeRange(w http.ResponseWriter, r *http.Requ

response := responseDTO.NewMultiEventsResponse("", "", http.StatusOK, totalCount, events)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (ec *EventController) DeleteEventsByAge(w http.ResponseWriter, r *http.Request) {
Expand All @@ -297,5 +297,5 @@ func (ec *EventController) DeleteEventsByAge(w http.ResponseWriter, r *http.Requ
response := commonDTO.NewBaseResponse("", "", http.StatusAccepted)
utils.WriteHttpHeader(w, ctx, http.StatusAccepted)
// encode and send out the response
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}
20 changes: 10 additions & 10 deletions internal/core/data/controller/http/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (rc *ReadingController) ReadingTotalCount(w http.ResponseWriter, r *http.Re

response := commonDTO.NewCountResponse("", "", http.StatusOK, count)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc) // encode and send out the countResponse
pkg.EncodeAndWriteResponse(response, w, lc) // encode and send out the countResponse
}

func (rc *ReadingController) AllReadings(w http.ResponseWriter, r *http.Request) {
Expand All @@ -74,7 +74,7 @@ func (rc *ReadingController) AllReadings(w http.ResponseWriter, r *http.Request)

response := responseDTO.NewMultiReadingsResponse("", "", http.StatusOK, totalCount, readings)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (rc *ReadingController) ReadingsByTimeRange(w http.ResponseWriter, r *http.Request) {
Expand All @@ -96,7 +96,7 @@ func (rc *ReadingController) ReadingsByTimeRange(w http.ResponseWriter, r *http.

response := responseDTO.NewMultiReadingsResponse("", "", http.StatusOK, totalCount, readings)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (rc *ReadingController) ReadingsByResourceName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -121,7 +121,7 @@ func (rc *ReadingController) ReadingsByResourceName(w http.ResponseWriter, r *ht

response := responseDTO.NewMultiReadingsResponse("", "", http.StatusOK, totalCount, readings)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (rc *ReadingController) ReadingsByDeviceName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -146,7 +146,7 @@ func (rc *ReadingController) ReadingsByDeviceName(w http.ResponseWriter, r *http

response := responseDTO.NewMultiReadingsResponse("", "", http.StatusOK, totalCount, readings)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (rc *ReadingController) ReadingCountByDeviceName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -168,7 +168,7 @@ func (rc *ReadingController) ReadingCountByDeviceName(w http.ResponseWriter, r *

response := commonDTO.NewCountResponse("", "", http.StatusOK, count)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc) // encode and send out the response
pkg.EncodeAndWriteResponse(response, w, lc) // encode and send out the response
}

// ReadingsByResourceNameAndTimeRange returns readings by resource name and specified time range. Readings are sorted in descending order of origin time.
Expand All @@ -194,7 +194,7 @@ func (rc *ReadingController) ReadingsByResourceNameAndTimeRange(w http.ResponseW

response := responseDTO.NewMultiReadingsResponse("", "", http.StatusOK, totalCount, readings)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (rc *ReadingController) ReadingsByDeviceNameAndResourceName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -220,7 +220,7 @@ func (rc *ReadingController) ReadingsByDeviceNameAndResourceName(w http.Response

response := responseDTO.NewMultiReadingsResponse("", "", http.StatusOK, totalCount, readings)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (rc *ReadingController) ReadingsByDeviceNameAndResourceNameAndTimeRange(w http.ResponseWriter, r *http.Request) {
Expand All @@ -247,7 +247,7 @@ func (rc *ReadingController) ReadingsByDeviceNameAndResourceNameAndTimeRange(w h

response := responseDTO.NewMultiReadingsResponse("", "", http.StatusOK, totalCount, readings)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (rc *ReadingController) ReadingsByDeviceNameAndResourceNamesAndTimeRange(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -298,5 +298,5 @@ func (rc *ReadingController) ReadingsByDeviceNameAndResourceNamesAndTimeRange(w

response := responseDTO.NewMultiReadingsResponse("", "", http.StatusOK, totalCount, readings)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}
16 changes: 8 additions & 8 deletions internal/core/metadata/controller/http/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (dc *DeviceController) AddDevice(w http.ResponseWriter, r *http.Request) {
}

utils.WriteHttpHeader(w, ctx, http.StatusMultiStatus)
pkg.Encode(addResponses, w, lc)
pkg.EncodeAndWriteResponse(addResponses, w, lc)
}

func (dc *DeviceController) DeleteDeviceByName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -100,7 +100,7 @@ func (dc *DeviceController) DeleteDeviceByName(w http.ResponseWriter, r *http.Re

response := commonDTO.NewBaseResponse("", "", http.StatusOK)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (dc *DeviceController) DevicesByServiceName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -125,7 +125,7 @@ func (dc *DeviceController) DevicesByServiceName(w http.ResponseWriter, r *http.

response := responseDTO.NewMultiDevicesResponse("", "", http.StatusOK, totalCount, devices)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (dc *DeviceController) DeviceNameExists(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -153,7 +153,7 @@ func (dc *DeviceController) DeviceNameExists(w http.ResponseWriter, r *http.Requ
statusCode = http.StatusNotFound
}
utils.WriteHttpHeader(w, ctx, statusCode)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (dc *DeviceController) PatchDevice(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -195,7 +195,7 @@ func (dc *DeviceController) PatchDevice(w http.ResponseWriter, r *http.Request)
}

utils.WriteHttpHeader(w, ctx, http.StatusMultiStatus)
pkg.Encode(updateResponses, w, lc)
pkg.EncodeAndWriteResponse(updateResponses, w, lc)
}

func (dc *DeviceController) AllDevices(w http.ResponseWriter, r *http.Request) {
Expand All @@ -217,7 +217,7 @@ func (dc *DeviceController) AllDevices(w http.ResponseWriter, r *http.Request) {

response := responseDTO.NewMultiDevicesResponse("", "", http.StatusOK, totalCount, devices)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (dc *DeviceController) DeviceByName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -236,7 +236,7 @@ func (dc *DeviceController) DeviceByName(w http.ResponseWriter, r *http.Request)

response := responseDTO.NewDeviceResponse("", "", http.StatusOK, device)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (dc *DeviceController) DevicesByProfileName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -261,5 +261,5 @@ func (dc *DeviceController) DevicesByProfileName(w http.ResponseWriter, r *http.

response := responseDTO.NewMultiDevicesResponse("", "", http.StatusOK, totalCount, devices)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}
22 changes: 11 additions & 11 deletions internal/core/metadata/controller/http/deviceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (dc *DeviceProfileController) AddDeviceProfile(w http.ResponseWriter, r *ht
}

utils.WriteHttpHeader(w, ctx, http.StatusMultiStatus)
pkg.Encode(addResponses, w, lc)
pkg.EncodeAndWriteResponse(addResponses, w, lc)
}

func (dc *DeviceProfileController) UpdateDeviceProfile(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -129,7 +129,7 @@ func (dc *DeviceProfileController) UpdateDeviceProfile(w http.ResponseWriter, r
}

utils.WriteHttpHeader(w, ctx, http.StatusMultiStatus)
pkg.Encode(responses, w, lc)
pkg.EncodeAndWriteResponse(responses, w, lc)
}

func (dc *DeviceProfileController) AddDeviceProfileByYaml(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -165,8 +165,8 @@ func (dc *DeviceProfileController) AddDeviceProfileByYaml(w http.ResponseWriter,

response := commonDTO.NewBaseWithIdResponse("", "", http.StatusCreated, newId)
utils.WriteHttpHeader(w, ctx, http.StatusCreated)
// Encode and send the resp body as JSON format
pkg.Encode(response, w, lc)
// EncodeAndWriteResponse and send the resp body as JSON format
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (dc *DeviceProfileController) UpdateDeviceProfileByYaml(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -202,7 +202,7 @@ func (dc *DeviceProfileController) UpdateDeviceProfileByYaml(w http.ResponseWrit

response := commonDTO.NewBaseResponse("", "", http.StatusOK)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (dc *DeviceProfileController) DeviceProfileByName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -221,7 +221,7 @@ func (dc *DeviceProfileController) DeviceProfileByName(w http.ResponseWriter, r

response := responseDTO.NewDeviceProfileResponse("", "", http.StatusOK, deviceProfile)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc) // encode and send out the response
pkg.EncodeAndWriteResponse(response, w, lc) // encode and send out the response
}

func (dc *DeviceProfileController) DeleteDeviceProfileByName(w http.ResponseWriter, r *http.Request) {
Expand All @@ -240,7 +240,7 @@ func (dc *DeviceProfileController) DeleteDeviceProfileByName(w http.ResponseWrit

response := commonDTO.NewBaseResponse("", "", http.StatusOK)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (dc *DeviceProfileController) AllDeviceProfiles(w http.ResponseWriter, r *http.Request) {
Expand All @@ -262,7 +262,7 @@ func (dc *DeviceProfileController) AllDeviceProfiles(w http.ResponseWriter, r *h

response := responseDTO.NewMultiDeviceProfilesResponse("", "", http.StatusOK, totalCount, deviceProfiles)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (dc *DeviceProfileController) DeviceProfilesByModel(w http.ResponseWriter, r *http.Request) {
Expand All @@ -287,7 +287,7 @@ func (dc *DeviceProfileController) DeviceProfilesByModel(w http.ResponseWriter,

response := responseDTO.NewMultiDeviceProfilesResponse("", "", http.StatusOK, totalCount, deviceProfiles)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (dc *DeviceProfileController) DeviceProfilesByManufacturer(w http.ResponseWriter, r *http.Request) {
Expand All @@ -312,7 +312,7 @@ func (dc *DeviceProfileController) DeviceProfilesByManufacturer(w http.ResponseW

response := responseDTO.NewMultiDeviceProfilesResponse("", "", http.StatusOK, totalCount, deviceProfiles)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}

func (dc *DeviceProfileController) DeviceProfilesByManufacturerAndModel(w http.ResponseWriter, r *http.Request) {
Expand All @@ -338,5 +338,5 @@ func (dc *DeviceProfileController) DeviceProfilesByManufacturerAndModel(w http.R

response := responseDTO.NewMultiDeviceProfilesResponse("", "", http.StatusOK, totalCount, deviceProfiles)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}
2 changes: 1 addition & 1 deletion internal/core/metadata/controller/http/deviceresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ func (dc *DeviceResourceController) DeviceResourceByProfileNameAndResourceName(w

response := responseDTO.NewDeviceResourceResponse("", "", http.StatusOK, resource)
utils.WriteHttpHeader(w, ctx, http.StatusOK)
pkg.Encode(response, w, lc)
pkg.EncodeAndWriteResponse(response, w, lc)
}
Loading