diff --git a/Readme.md b/Readme.md index a2c95c3..1a9a22e 100644 --- a/Readme.md +++ b/Readme.md @@ -6,6 +6,25 @@ This is module to help you make web requests in Go, it is a wrapper around the standard library's `http` package. You can use Get or Post to make a request, and then use the `Response` object to get the response body, headers, status code, etc. +### Example + + package main + + import ( + "fmt" + "github.com/tonnytg/webreq" + ) + + func main() { + response, err := webreq.Get("https://api.example.com/data") + if err != nil { + fmt.Println("Error:", err) + return + } + fmt.Println("Response:", response) + } + + ## Why So many times I needed to make a request in an API and after convert body to struct, and every time I needed to configure http.Client and put headers and body. Thinking about "don't repeat your self" I believe this unified code can gain time build an easy way to make this request. @@ -13,49 +32,104 @@ So many times I needed to make a request in an API and after convert body to str What do you think? Liked, set star in this project to help me to help others! -### Install +## Install > go get github.com/tonnytg/webreq +## Basic Usage + ### Get Create a slice of headers to use and input in request - headers := webreq.NewHeaders() - headers.Add("Content-Type", "application/json") - - request := webreq.Builder("GET") - request.SetURL("https://610aa52552d56400176afebe.mockapi.io/api/v1/friendlist") - - _, err := request.Execute() - if err != nil { - t.Error(err) - } + package main + + import ( + "fmt" + "github.com/tonnytg/webreq" + ) + + func main() { + response, err := webreq.Get("https://api.example.com/data") + if err != nil { + fmt.Println("Error:", err) + return + } + fmt.Println("Response:", response) + } ### Post Create a body data to send in request - ... - f := Friend{ - CreatedAt: time.Now(), - Name: "Tonny", - } - - // convert f to bytes - fBytes, err := json.Marshal(f) - if err != nil { - t.Error(err) - } - - request := webreq.Builder("POST") - request.SetURL("https://610aa52552d56400176afebe.mockapi.io/api/v1/friendlist") - request.SetBody(fBytes) - - body, err := request.Execute() - if err != nil { - t.Error(err) - } - ... + package main + import ( + "fmt" + "github.com/tonnytg/webreq" + ) + + func main() { + data := map[string]string{"key": "value"} + response, err := webreq.Post("https://api.example.com/data", data) + if err != nil { + fmt.Println("Error:", err) + return + } + fmt.Println("Response:", response) + } + + + +## Advange Usage + + +### Custom Headers + + headers := map[string]string{ + "Content-Type": "application/json", + "Authorization": "Bearer your_token", + } + + response, err := webreq.GetWithHeaders("https://api.example.com/data", headers) + if err != nil { + fmt.Println("Error:", err) + return + } + fmt.Println("Response:", response) + + + +### Erro Handlin + + response, err := webreq.Get("https://api.example.com/data") + if err != nil { + switch err.(type) { + case webreq.HTTPError: + fmt.Println("HTTP error occurred:", err) + default: + fmt.Println("An error occurred:", err) + } + return + } + fmt.Println("Response:", response) + + +## Contributing +- Fork the repository +- Create a new branch (git checkout -b feature-foo) +- Commit your changes (git commit -am 'Add some foo') +- Push to the branch (git push origin feature-foo) +- Create a new Pull Request + + +## License +This project is licensed under the MIT License. + + +## Contact + +For any inquiries, please open an issue or reach out to the maintainer. + +email: tonnytg@gmail.com