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

Handling Response Status Codes of HTTP #214

Open
sortira opened this issue Sep 24, 2024 · 5 comments
Open

Handling Response Status Codes of HTTP #214

sortira opened this issue Sep 24, 2024 · 5 comments

Comments

@sortira
Copy link

sortira commented Sep 24, 2024

In the Do function of the Pipe object, all HTTP response codes of 2xx are treated as okay. While this is a good generalisation, a possible enhancement could be to create a dictionary containing helpful messages for the various HTTP codes and display the appropriate ones for completeness sake and provide finer control to the user who might want to perform different tasks based on different response codes.

func (p *Pipe) Do(req *http.Request) *Pipe {

@bitfield
Copy link
Owner

What did you have in mind exactly? Could you provide an example program and show what it would produce?

@sortira
Copy link
Author

sortira commented Sep 26, 2024

What did you have in mind exactly? Could you provide an example program and show what it would produce?

Not sure if the repo would accept a PR, I could create one and the code could be reviewed.

@bitfield
Copy link
Owner

Just post it here as a comment if you like.

@sortira
Copy link
Author

sortira commented Sep 26, 2024

@bitfield here you go.

var httpStatusMessages = map[int]string{
	200: "OK",
	201: "Created",
	202: "Accepted",
	204: "No Content",
	400: "Bad Request",
	401: "Unauthorized",
	403: "Forbidden",
	404: "Not Found",
	500: "Internal Server Error",
	502: "Bad Gateway",
	503: "Service Unavailable",
	504: "Gateway Timeout",
}
func (p *Pipe) Do(req *http.Request) *Pipe {
	return p.Filter(func(r io.Reader, w io.Writer) error {
		resp, err := p.httpClient.Do(req)
		if err != nil {
			return err
		}
		defer resp.Body.Close()

		_, err = io.Copy(w, resp.Body)
		if err != nil {
			return err
		}

		// Get the status message from the dictionary, or a default message if not found
		statusMessage, found := httpStatusMessages[resp.StatusCode]
		if !found {
			statusMessage = "Unknown Status Code"
		}

		// Any HTTP 2xx status code is considered okay
		if resp.StatusCode/100 != 2 {
			return fmt.Errorf("unexpected HTTP response status: %s (%s)", resp.Status, statusMessage)
		}

		fmt.Printf("HTTP response status: %s (%s)\n", resp.Status, statusMessage)
		return nil
	})
}

PS: This repo was suggested by the good-first-issues label for my Hacktoberfest preparation, so feedbacks will be welcome!

@bitfield
Copy link
Owner

Right, but the question is how would such a feature be used? You mentioned:

provide finer control to the user who might want to perform different tasks based on different response codes.

So can you write an example program that does this? Then we'll see if the API is a good fit for the problem or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants