HTTP calls' status code handling #10

Open
opened 2025-03-11 15:53:23 +01:00 by pb · 1 comment
Owner

We don't have enough oversight on our HTTP calls in tools.remote_caller.go. We only check if http.Post and http.NewRequest return an error, but don't use the http.Response.StatusCode attribute.

I suggest that we get the response status code handling closer from the calling method, by declaring the expected response status code in the HTTPCaller constructor for each method of a ressource, like this :

tools.NewHTTPCaller(
       map[tools.DataType]map[tools.METHOD]string{
			tools.ADMIRALTY_SOURCE: map[tools.METHOD]string{
				tools.POST : "/:id",
				success_code : http.StatusOK     // <---- HERE
			},
}
We don't have enough oversight on our HTTP calls in tools.remote_caller.go. We only check if http.Post and http.NewRequest return an error, but don't use the http.Response.StatusCode attribute. I suggest that we get the response status code handling closer from the calling method, by declaring the expected response status code in the HTTPCaller constructor for each method of a ressource, like this : ``` tools.NewHTTPCaller( map[tools.DataType]map[tools.METHOD]string{ tools.ADMIRALTY_SOURCE: map[tools.METHOD]string{ tools.POST : "/:id", success_code : http.StatusOK // <---- HERE }, } ```
Author
Owner

Implemented a first draft of how to handle the result of an HTTP call with a new attribute LastExecution map[string]interface{} in HTTPCaller which can receive the different attribute from the http.Response, however we must think of a way of passing a list of attribute we want to retrieve from the Response.

Currently, the HTTPCaller only stores the result for its last call, in a single attribute, which could be a problem if it is shared by several threads

For the Call to APIs we just implemented it like this :

func (caller *HTTPCaller) StoreResp(resp *http.Response) error {
	caller.LastResults = make(map[string]interface{})
	caller.LastResults["header"] = resp.Header
	caller.LastResults["code"] = resp.StatusCode
	data, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading the body of the last request")
		return err
	}

	caller.LastResults["body"] = data
	return nil
}
Implemented a first draft of how to handle the result of an HTTP call with a new attribute `LastExecution map[string]interface{}` in HTTPCaller which can receive the different attribute from the http.Response, however we must think of a way of passing a list of attribute we want to retrieve from the Response. Currently, the HTTPCaller only stores the result for its last call, in a single attribute, which could be a problem **if it is shared by several threads** For the Call to APIs we just implemented it like this : ``` func (caller *HTTPCaller) StoreResp(resp *http.Response) error { caller.LastResults = make(map[string]interface{}) caller.LastResults["header"] = resp.Header caller.LastResults["code"] = resp.StatusCode data, err := io.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading the body of the last request") return err } caller.LastResults["body"] = data return nil } ```
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: core/oc-lib#10
No description provided.