Skip to content

Commit

Permalink
Add LICENCE, CHANGELOG and Update Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
pzentenoe committed Apr 28, 2024
1 parent b23d171 commit 58d8b03
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 56 deletions.
43 changes: 43 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.15.0] - 2022-11-17
### Added
- Initial release of the `httpclient-call-go`.
- Release HTTP connection library.
- Testing.
- golangcilint.yml

## [1.17.0] - 2022-11-17
### Added
- Initial release of the `httpclient-call-go`.
- Release HTTP connection library.
- Testing.

## [1.19.0] - 2022-11-17
### Added
- Initial release of the `httpclient-call-go`.
- Release HTTP connection library.
- Testing.


## [1.20.0] - 2023-06-23
### Changed
- Update dependencies
- Update go.mod
- Update README.md

## [1.22.0] - 2024-04-09
### Changed
- Update dependencies
- Update go.mod
- Update README.md
- Add new testing
- Update golangci.yml
- Add github actions
- Add CHANGELOG file
- Add LICENCE
22 changes: 22 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

MIT License

Copyright (c) 2024 Pablo Zenteno

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
140 changes: 84 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# httpclient-call-go

The `httpclient-call-go` library simplifies making HTTP calls to various API services efficiently and straightforwardly. It is designed to seamlessly integrate into any Go project requiring HTTP API interactions.
## Overview

The `httpclient-call-go` library simplifies making HTTP calls to various API services efficiently and straightforwardly.
It is designed to seamlessly integrate into any Go project requiring HTTP API interactions.

![Actions](https://github.com/pzentenoe/httpclient-call-go/actions/workflows/actions.yml/badge.svg)
![Build](https://github.com/pzentenoe/httpclient-call-go/actions/workflows/actions.yml/badge.svg)

### Buy Me a Coffee


<a href="https://www.buymeacoffee.com/pzentenoe" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>

Thank you for your support! ❤️
Expand All @@ -18,115 +20,141 @@ Thank you for your support! ❤️
- Full support for customizing HTTP requests (headers, body, timeouts).
- Convenient methods for making HTTP calls and deserializing JSON responses.


## Installation

To use `httpclient-call-go` in your project, install it using the following Go command:

```bash
go get github.com/pzentenoe/httpclient-call-go
```

## Quick Start

### Setting Up HTTP Client
First, import the library and create a new instance of HTTPClientCall specifying the base URL of the API service and an HTTP client:

First, import the library and create a new instance of HTTPClientCall specifying the base URL of the API service and an
HTTP client:

```go
import (
"net/http"
client "github.com/pzentenoe/httpclient-call-go"
"net/http"
client "github.com/pzentenoe/httpclient-call-go"
)

httpClientCall := client.NewHTTPClientCall("https://dummyhost.cl", &http.Client{})
```

## Making an HTTP Call

Using **Do** Implementation

To perform a simple POST request and handle the response as []byte:

```go
package main

import (
"context"
"fmt"
"io"
"net/http"
"time"
client "github.com/pzentenoe/httpclient-call-go"
"context"
"fmt"
"io"
"net/http"
"time"
client "github.com/pzentenoe/httpclient-call-go"
)

func main() {
httpClientCall := client.NewHTTPClientCall("https://dummyhost.cl", &http.Client{})
headers := http.Header{
client.HeaderContentType: []string{client.MIMEApplicationJSON},
}
dummyBody := map[string]interface{}{"age": 30, "name": "test"}
httpClientCall := client.NewHTTPClientCall("https://dummyhost.cl", &http.Client{})
headers := http.Header{
client.HeaderContentType: []string{client.MIMEApplicationJSON},
}
dummyBody := map[string]interface{}{"age": 30, "name": "test"}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

response, err := httpClientCall.
response, err := httpClientCall.
Method(http.MethodPost).
Path("/path").
Body(dummyBody).
Body(dummyBody).
Headers(headers).
Do(ctx)
if err != nil {
fmt.Println("Error calling the API:", err)
return
}
defer response.Body.Close()

dataBytes, errToRead := io.ReadAll(response.Body)
if errToRead != nil {
fmt.Println("Error reading data:", errToRead)
return
}
fmt.Println(string(dataBytes))
if err != nil {
fmt.Println("Error calling the API:", err)
return
}
defer response.Body.Close()

dataBytes, errToRead := io.ReadAll(response.Body)
if errToRead != nil {
fmt.Println("Error reading data:", errToRead)
return
}
fmt.Println(string(dataBytes))
}
```

Using **DoWithUnmarshal** Implementation

To perform a POST request and automatically deserialize the JSON response into a Go structure:

```go
package main

import (
"context"
"fmt"
"net/http"
"time"
client "github.com/pzentenoe/httpclient-call-go"
"context"
"fmt"
"net/http"
"time"
client "github.com/pzentenoe/httpclient-call-go"
)

type someBodyResponse struct {
Name string `json:"name"`
Name string `json:"name"`
}

func main() {
httpClientCall := client.NewHTTPClientCall("https://dummyhost.cl", &http.Client{})
headers := http.Header{
client.HeaderContentType: []string{client.MIMEApplicationJSON},
}
dummyBody := map[string]interface{}{"age": 30, "name": "test"}
httpClientCall := client.NewHTTPClientCall("https://dummyhost.cl", &http.Client{})
headers := http.Header{
client.HeaderContentType: []string{client.MIMEApplicationJSON},
}
dummyBody := map[string]interface{}{"age": 30, "name": "test"}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

var responseBody someBodyResponse
resp, err := httpClientCall.
var responseBody someBodyResponse
resp, err := httpClientCall.
Method(http.MethodPost).
Path("/path").
Body(dummyBody).
Body(dummyBody).
Headers(headers).
DoWithUnmarshal(ctx, &responseBody)
if err != nil {
fmt.Println("Error calling the API:", err)
return
}
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println("Name in Response:", responseBody.Name)
if err != nil {
fmt.Println("Error calling the API:", err)
return
}
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println("Name in Response:", responseBody.Name)
}
```

## Autor
## Testing

Execute the tests with:

```bash
go test ./...
```

## Contributing
We welcome contributions! Please fork the project and submit pull requests to the `main` branch. Make sure to add tests
for new functionalities and document any significant changes.

## License
This project is released under the MIT License. See the [LICENSE](LICENSE) file for more details.

## Changelog
For a detailed changelog, refer to [CHANGELOG.md](CHANGELOG.md).

## Autor
- **Pablo Zenteno** - _Full Stack Developer_ - [pzentenoe](https://github.com/pzentenoe)

0 comments on commit 58d8b03

Please sign in to comment.