hammer
hammer copied to clipboard
Hammer

Golang's Fluent HTTP Request Client

Recipes
client := hammer.New()
request, err := hammer.RequestBuilder().
<HttpVerb>().
WithURL("http://localhost:8081/employee").
WithContext(context.Background()).
WithHeaders("Accept", "application/json").
WithHeaders("user-id", "10062").
WithRequestParams("department", "HR").
Build()
resp, err:= client.Execute(request)
// or
responseMap := make(map[string]interface{})
err:= client.ExecuteInto(request, &responseMap)
// or
responseModel := Employee{}
err:= client.ExecuteInto(request, &responseModel)
Supported HTTP Verbs
Get()
Head()
Post()
PostForm()
Put()
Patch()
Delete()
Connect()
Options()
Trace()
Hammer Client Api's
// New intializes and returns new Hammer Client
New()
// WithHTTPClient returns Hammer client with custom HTTPClient
WithHTTPClient(*http.Client)
// Execute the Request
Execute(*Request)
// Execute the Request and unmarshal into map or struct provided with unmarshalInto. Please See recipes.
ExecuteInto(*Request,unmarshalInto interface{})
RequestBuilder Api's
// WithRequestBody struct or map can be sent
WithRequestBody(body interface{})
// WithContext ...
WithContext(ctx context.Context)
// WithHeaders ...
WithHeaders(key string, value string)
// WithRequestParams ...
WithRequestParams(key string, value string)
// WithRequestBodyParams ...
WithRequestBodyParams(key string, value interface{})
// WithFormValues ...
WithFormValues(Key string, value interface{})
// WithURL ...
WithURL(value string)
// WithBasicAuth ...
WithBasicAuth(username, password string)
// WithTemplate will create a request with already created request. See example below.
WithTemplate(tempRequest *Request)
client := hammer.New()
request, err := hammer.RequestBuilder().
Get().
WithURL("http://localhost:8081/employee").
WithHeaders("Accept", "application/json").
WithHeaders("user-id", "10062").
WithRequestParams("department", "HR").
Build()
employeeList := []Employee{}
err := client.ExecuteInto(request,&EmployeeList)
client := hammer.New()
reqTemp, err := hammer.RequestBuilder().
Get().
WithURL("http://localhost:8081/employee").
WithHeaders("Accept", "application/json").
WithHeaders("user-id", "10062").
WithRequestParams("department", "HR").
Build()
request, err := hammer.RequestBuilder().
WithTemplate(reqTemp).
WithRequestParams("post","manager").
WithRequestParams("centre","pune")
Build()
employeeList := []Employee{}
err:= client.ExecuteInto(request,&EmployeeList)
Contributing
- Fork the repo and create your branch from master.
- If you've added code that should be tested, add tests.
- If you've changed APIs, update the documentation.
- Ensure the test suite passes.
- Make sure your code lints.
- Issue that pull request!
View CONTRIBUTING.md to learn more about how to contribute.
License
This project is open source and available under the MIT License.