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

feat: Add the post with headers util function for HTTP clients #955

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions clients/http/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ func createRequestWithRawData(ctx context.Context, httpMethod string, baseUrl st
return req, nil
}

func createRequestWithRawDataAndHeaders(ctx context.Context, httpMethod string, baseUrl string, requestPath string, requestParams url.Values, data any, headers map[string]string) (*http.Request, errors.EdgeX) {
req, err := createRequestWithRawData(ctx, httpMethod, baseUrl, requestPath, requestParams, data)
if err != nil {
return nil, errors.NewCommonEdgeXWrapper(err)
}

// Add the additional headers from request
for name, value := range headers {
req.Header.Set(name, value)
}

return req, nil
}

func createRequestWithEncodedData(ctx context.Context, httpMethod string, baseUrl string, requestPath string, data []byte, encoding string) (*http.Request, errors.EdgeX) {
u, err := parseBaseUrlAndRequestPath(baseUrl, requestPath)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions clients/http/utils/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ func PostRequestWithRawData(
return processRequest(ctx, returnValuePointer, req, authInjector)
}

// PostRequestWithRawDataAndHeaders makes the post JSON request with raw data and request headers, and returns the body
func PostRequestWithRawDataAndHeaders(
ctx context.Context,
returnValuePointer interface{},
baseUrl string, requestPath string,
requestParams url.Values,
data interface{}, authInjector interfaces.AuthenticationInjector,
headers map[string]string) errors.EdgeX {

req, err := createRequestWithRawDataAndHeaders(ctx, http.MethodPost, baseUrl, requestPath, requestParams, data, headers)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}

return processRequest(ctx, returnValuePointer, req, authInjector)
}

// PutRequest makes the put JSON request and return the body
func PutRequest(
ctx context.Context,
Expand Down
5 changes: 4 additions & 1 deletion errors/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
KindOverflowError ErrKind = "OverflowError"
KindNaNError ErrKind = "NaNError"
KindUnauthorized ErrKind = "Unauthorized"
KindForbidden ErrKind = "Forbidden"
)

// EdgeX provides an abstraction for all internal EdgeX errors.
Expand Down Expand Up @@ -209,7 +210,7 @@ func codeMapping(kind ErrKind) int {
return http.StatusMethodNotAllowed
case KindRangeNotSatisfiable:
return http.StatusRequestedRangeNotSatisfiable
case KindIOError:
case KindIOError, KindForbidden:
return http.StatusForbidden
case KindUnauthorized:
return http.StatusUnauthorized
Expand Down Expand Up @@ -245,6 +246,8 @@ func KindMapping(code int) ErrKind {
return KindRangeNotSatisfiable
case http.StatusUnauthorized:
return KindUnauthorized
case http.StatusForbidden:
return KindForbidden
default:
return KindUnknown
}
Expand Down