Simple HTTP and REST client library for Go, Inspired by Guzzle HTTP client from PHP.
go get github.com/sheenazien8/vortex@v1.0.0
package main
import (
"github.com/sheenazien8/vortex"
"log"
)
func main() {
var response struct {
Data struct {
Email string `json:"email"`
Token string `json:"token"`
} `json:"data"`
Message string `json:"message"`
Status bool `json:"status"`
}
apiClient := vortex.New(vortex.Opt{
BaseURL: "https://lakasir.test",
})
resp, err := apiClient.
SetHeader("Content-Type", "application/json").
SetHeader("Accept", "application/json").
SetOutput(&response).
Post("/api/auth/login", map[string]interface{}{
"email": "warunghikmah@lakasir.com",
"password": "password",
})
if err != nil {
panic(err)
}
if resp.StatusCode != 200 {
log.Fatal("status code is not 200 ", resp.StatusCode, string(resp.Body))
}
data := response.Data
var token = data.Token
meResponse, err := apiClient.
SetHeader("Content-Type", "application/json").
SetHeader("Accept", "application/json").
SetHeader("Authorization", "Bearer "+token).
SetOutput(&response).
Get("/api/auth/me")
if err != nil {
panic(err)
}
if meResponse.StatusCode != 200 {
log.Fatal("status code is not 200 ", meResponse.StatusCode, string(meResponse.Body))
}
println(response.Data.Email)
}
curlCommand := resp.Request.GenerateCurlCommand()
println("Generated Curl Command:", curlCommand)
func LoggingMiddleware(req *http.Request, next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("Request: %s %s", r.Method, r.URL.String())
next.ServeHTTP(w, r)
log.Printf("Response: %s", w.Header().Get("StatusCode"))
}
}
apiClient := vortex.New(vortex.Opt{
BaseURL: "https://lakasir.test",
})
resp, err := apiClient.
UseMiddleware(LoggingMiddleware)
func ExampleHook(req *http.Request, resp *http.Response) {
log.Printf("Hook: Response status code: %d", resp.StatusCode)
}
apiClient := vortex.New(vortex.Opt{
BaseURL: "https://lakasir.test",
})
resp, err := apiClient.
UseHook(ExampleHook)
func streamRequest(resp *http.Response) {
log.Printf("Stream: Response status code: %d", resp.StatusCode)
}
apiClient := vortex.New(vortex.Opt{
BaseURL: "https://lakasir.test",
})
_, err := apiClient.
Stream(streamRequest).
Post("/test")
apiClient := vortex.New(vortex.Opt{
BaseURL: "https://lakasir.test",
})
uploadRes, err := apiClient.
SetHeader("Accept", "application/json").
SetFormFilePath("file", "./test_image.png").
Post("/api/temp/upload", nil)
apiClient := vortex.New(vortex.Opt{
BaseURL: "https://lakasir.test",
})
pathFile := "./test_image.png"
file, err := os.Open(pathFile)
if err != nil {
panic(err)
}
uploadRes, err := apiClient.
SetHeader("Accept", "application/json").
SetFormFile("file", file).
Post("/api/temp/upload", nil)
We welcome contributions to the Vortex project! If you would like to contribute, please follow these guidelines:
Fork the repository: Click the “Fork” button at the top right of this repository to create a copy of the repository in your GitHub account.
git clone https://github.com/sheenazien8/vortex.git
cd vortex
git checkout -b my-feature-branch
Make your changes: Make your changes to the codebase. Ensure that your code follows the project’s coding standards and includes appropriate tests.
git add .
git commit -m "Add feature X"
git push origin my-feature-branch
Create a pull request: Go to the original repository on GitHub and create a pull request from your forked repository. Provide a clear description of your changes and any related issues.
Thank you for contributing to Vortex!
This project is licensed under the MIT License. See the LICENSE file for more details.