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

add Post Sync and Options async + sync, with and without callback #499

Merged
merged 2 commits into from
Sep 2, 2024
Merged
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
61 changes: 55 additions & 6 deletions session/resthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ const (
PATCH
// HEAD RestMethod
HEAD
// OPTIONS RestMethod
OPTIONS
)

var (
Expand All @@ -127,12 +129,13 @@ var (

var (
restMethodEnumMap = enummap.NewEnumMapOrPanic(map[string]int{
"get": int(GET),
"post": int(POST),
"delete": int(DELETE),
"put": int(PUT),
"patch": int(PATCH),
"head": int(HEAD),
"get": int(GET),
"post": int(POST),
"delete": int(DELETE),
"put": int(PUT),
"patch": int(PATCH),
"head": int(HEAD),
"options": int(OPTIONS),
})

defaultReqOptions = ReqOptions{
Expand Down Expand Up @@ -443,6 +446,11 @@ func (handler *RestHandler) PostAsync(url string, actionState *action.State, log
return handler.PostAsyncWithCallback(url, actionState, logEntry, content, nil, options, nil)
}

// PostSync send sync POST request with options, using options=nil default options are used
func (handler *RestHandler) PostSync(url string, actionState *action.State, logEntry *logger.LogEntry, content []byte, options *ReqOptions) (*RestRequest, error) {
return handler.PostSyncWithCallback(url, actionState, logEntry, content, nil, options, nil)
}

// PostWithHeadersAsync send async POST request with options and headers, using options=nil default options are used
func (handler *RestHandler) PostWithHeadersAsync(url string, actionState *action.State, logEntry *logger.LogEntry, content []byte, headers map[string]string, options *ReqOptions) *RestRequest {
return handler.PostAsyncWithCallback(url, actionState, logEntry, content, headers, options, nil)
Expand All @@ -453,6 +461,11 @@ func (handler *RestHandler) PostAsyncWithCallback(url string, actionState *actio
return handler.sendAsyncWithCallback(POST, url, actionState, logEntry, content, headers, options, callback)
}

// PostSyncWithCallback send sync POST request with options and callback, using options=nil default options are used
func (handler *RestHandler) PostSyncWithCallback(url string, actionState *action.State, logEntry *logger.LogEntry, content []byte, headers map[string]string, options *ReqOptions, callback func(err error, req *RestRequest)) (*RestRequest, error) {
return handler.sendSyncWithCallback(POST, url, actionState, logEntry, content, headers, options, callback)
}

// DeleteAsyncWithCallback send async DELETE request with options and callback, using options=nil default options are used
func (handler *RestHandler) DeleteAsyncWithCallback(url string, actionState *action.State, logEntry *logger.LogEntry, headers map[string]string, options *ReqOptions, callback func(err error, req *RestRequest)) *RestRequest {
return handler.sendAsyncWithCallback(DELETE, url, actionState, logEntry, nil, headers, options, callback)
Expand All @@ -468,6 +481,40 @@ func (handler *RestHandler) DeleteAsync(url string, actionState *action.State, l
return handler.DeleteAsyncWithCallback(url, actionState, logEntry, nil, options, nil)
}

// OptionsAsync send async request with options, using options=nil default options are used
func (handler *RestHandler) OptionsAsync(url string, actionState *action.State, logEntry *logger.LogEntry, headers map[string]string, options *ReqOptions) *RestRequest {
return handler.sendAsyncWithCallback(OPTIONS, url, actionState, logEntry, nil, headers, options, nil)
}

// OptionsAsyncWitCallback send async request with options and callback, using options=nil default options are used
func (handler *RestHandler) OptionsAsyncWitCallback(url string, actionState *action.State, logEntry *logger.LogEntry, headers map[string]string, options *ReqOptions, callback func(err error, req *RestRequest)) *RestRequest {
return handler.sendAsyncWithCallback(OPTIONS, url, actionState, logEntry, nil, headers, options, callback)
}

// OptionsSync send sync request with options, using options=nil default options are used
func (handler *RestHandler) OptionsSync(url string, actionState *action.State, logEntry *logger.LogEntry, headers map[string]string, options *ReqOptions) (*RestRequest, error) {
return handler.sendSyncWithCallback(OPTIONS, url, actionState, logEntry, nil, headers, options, nil)
}

// OptionsSyncWitCallback send sync request with options and callback, using options=nil default options are used
func (handler *RestHandler) OptionsSyncWitCallback(url string, actionState *action.State, logEntry *logger.LogEntry, headers map[string]string, options *ReqOptions, callback func(err error, req *RestRequest)) (*RestRequest, error) {
return handler.sendSyncWithCallback(OPTIONS, url, actionState, logEntry, nil, headers, options, callback)
}

func (handler *RestHandler) sendSyncWithCallback(method RestMethod, url string, actionState *action.State, logEntry *logger.LogEntry, content []byte, headers map[string]string, options *ReqOptions, callback func(err error, req *RestRequest)) (*RestRequest, error) {
var returnErr error
var wg sync.WaitGroup
wg.Add(1)
returnReq := handler.sendAsyncWithCallback(method, url, actionState, logEntry, content, headers, options, func(err error, req *RestRequest) {
defer wg.Done()
if callback != nil {
callback(err, req)
}
})
wg.Wait()
return returnReq, returnErr
}

func (handler *RestHandler) sendAsyncWithCallback(method RestMethod, url string, actionState *action.State, logEntry *logger.LogEntry, content []byte, headers map[string]string, options *ReqOptions, callback func(err error, req *RestRequest)) *RestRequest {
if options == nil {
options = &defaultReqOptions
Expand Down Expand Up @@ -672,6 +719,8 @@ func newStdRequest(ctx context.Context, request *RestRequest, logEntry *logger.L
req, err = http.NewRequest(http.MethodPut, request.Destination, getRequestReader(request))
case PATCH:
req, err = http.NewRequest(http.MethodPatch, request.Destination, getRequestReader(request))
case OPTIONS:
req, err = http.NewRequest(http.MethodOptions, request.Destination, nil)
default:
return nil, errors.Errorf("Unsupported REST method<%v>", request.Method)
}
Expand Down