Skip to content

Commit 904f670

Browse files
committed
first commit
0 parents  commit 904f670

6 files changed

Lines changed: 402 additions & 0 deletions

File tree

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Dependency directories (remove the comment below to include it)
18+
# vendor/
19+
20+
# Go workspace file
21+
go.work
22+
23+
main.go

README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
2+
# OpenApi IT Go Client
3+
4+
This client is used to interact with the API found at [openapi.it](https://openapi.it/)
5+
6+
## Pre-requisites
7+
8+
Before using the OpenApi IT Go Client, you will need an account at [openapi.it](https://openapi.it/) and an API key to the sandbox and/or production environment
9+
10+
## Installation
11+
12+
You can install the OpenApi IT Go Client with the following command using go get:
13+
14+
```bash
15+
go get github.com/openapi-it/openapi-cli-go
16+
```
17+
18+
## Usage
19+
20+
```go
21+
// main.go
22+
package main
23+
24+
import (
25+
client "github.com/openapi-it/openapi-cli-go/pkg/client"
26+
)
27+
28+
func main() {
29+
// Initialize the oauth client on the sandbox environment
30+
ctx := context.Background()
31+
oauthClient := client.NewOauthClient("<your_username>", "<your_apikey>", true)
32+
33+
// Create a token for a list of scopes
34+
scopes := []string{
35+
"GET:test.cap.openapi.it/cerca_comuni",
36+
"POST:test.postontarget.com/fields/country",
37+
}
38+
ttl := 2592000
39+
resp, err := oauthClient.CreateToken(ctx, scopes, ttl) // returns the json as string
40+
if err != nil {
41+
log.Fatal(err)
42+
}
43+
44+
// The string response can be parsed into a custom object
45+
tokenResponse := struct {
46+
Scopes []string `json:"scopes"`
47+
Token string `json:"token"`
48+
}{}
49+
_ = json.Unmarshal([]byte(resp), &tokenResponse)
50+
51+
// Initialize the client
52+
client := client.NewClient(tokenResponse.Token)
53+
54+
// Make a request with params
55+
params := map[string]string{
56+
"denominazione": "altravia",
57+
"provincia": "RM",
58+
"codice_ateco": "6201",
59+
}
60+
_, err = client.Request(
61+
ctx,
62+
"GET",
63+
"https://test.imprese.openapi.it",
64+
"/advance",
65+
nil, params,
66+
)
67+
if err != nil {
68+
log.Fatal(err)
69+
}
70+
71+
// Make a request with a payload
72+
payload := struct {
73+
Limit int `json:"limit"`
74+
Query struct {
75+
CountryCode string `json:"country_code"`
76+
} `json:"query"`
77+
}{
78+
Limit: 0,
79+
Query: struct {
80+
CountryCode string `json:"country_code"`
81+
}{CountryCode: "IT"},
82+
}
83+
var buf bytes.Buffer
84+
if err := json.NewEncoder(&buf).Encode(payload); err != nil {
85+
log.Fatal(err)
86+
}
87+
_, err = client.Request(
88+
ctx,
89+
"POST",
90+
"https://test.postontarget.com",
91+
"/fields/country",
92+
&buf,
93+
nil,
94+
)
95+
if err != nil {
96+
log.Fatal(err)
97+
}
98+
99+
// Delete the token
100+
_, err = oauthClient.DeleteToken(ctx, tokenResponse.Token)
101+
if err != nil {
102+
log.Fatal(err)
103+
}
104+
}
105+
```
106+
107+
## Contributing
108+
109+
Contributions are always welcome!
110+
111+
See `contributing.md` for ways to get started.
112+
113+
Please adhere to this project's `code of conduct`.
114+
115+
116+
## License
117+
118+
[MIT](https://choosealicense.com/licenses/mit/)
119+
120+
121+
## Authors
122+
123+
- [@maiku1008](https://www.github.com/maiku1008)
124+
- [@openapi-it](https://github.com/openapi-it)

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/openapi-it/openapi-cli-go
2+
3+
go 1.19

pkg/client/client.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"io"
6+
"net/http"
7+
"net/url"
8+
)
9+
10+
// NewClient creates a new Client instance with the given authentication token.
11+
// The returned client can be used to make API requests.
12+
func NewClient(token string) *Client {
13+
return &Client{
14+
authHeader: "Bearer " + token,
15+
httpClient: &http.Client{},
16+
}
17+
}
18+
19+
// Client is a simple HTTP client used to make API requests.
20+
type Client struct {
21+
authHeader string
22+
httpClient *http.Client
23+
}
24+
25+
// Request sends an HTTP request to the specified endpoint.
26+
// ctx: the context.Context to be used for the request.
27+
// baseURL: the base URL of the API.
28+
// method: the HTTP method to use (e.g. GET, POST, etc.).
29+
// endpoint: the endpoint to send the request to.
30+
// body: the body of the request.
31+
// params: the query parameters to include in the request.
32+
//
33+
// Returns a string containing the formatted JSON response and an error if any.
34+
func (c *Client) Request(
35+
ctx context.Context,
36+
method string,
37+
baseURL string,
38+
endpoint string,
39+
body io.Reader,
40+
params map[string]string) (string, error) {
41+
42+
queryParams := url.Values{}
43+
for key, value := range params {
44+
queryParams.Set(key, value)
45+
}
46+
opts := &requestOptions{
47+
method: method,
48+
baseURL: baseURL,
49+
endpoint: endpoint,
50+
authString: c.authHeader,
51+
payload: body,
52+
queryParams: queryParams,
53+
}
54+
return request(ctx, c.httpClient, opts)
55+
}

pkg/client/common.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package client
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"io"
8+
"net/http"
9+
"net/url"
10+
)
11+
12+
// httpClient is an interface that defines the Do method for making HTTP requests.
13+
type httpClient interface {
14+
Do(req *http.Request) (*http.Response, error)
15+
}
16+
17+
// requestOptions contains the options for an HTTP request.
18+
type requestOptions struct {
19+
method string // HTTP request method (e.g., "GET", "POST", etc.)
20+
baseURL string // Base URL for the HTTP request.
21+
endpoint string // Endpoint for the HTTP request.
22+
authString string // Authentication string for the HTTP request.
23+
payload io.Reader // Payload for the HTTP request (if any).
24+
queryParams url.Values // Query parameters for the HTTP request (if any).
25+
}
26+
27+
// request makes an HTTP request with the specified options.
28+
func request(
29+
ctx context.Context,
30+
client httpClient,
31+
opts *requestOptions) (string, error) {
32+
33+
req, err := http.NewRequestWithContext(ctx, opts.method, opts.baseURL, opts.payload)
34+
if err != nil {
35+
return "", err
36+
}
37+
req.Header.Add("Authorization", opts.authString)
38+
req.Header.Add("Content-Type", `application/json`)
39+
req.URL.Path += opts.endpoint
40+
41+
if opts.queryParams != nil {
42+
req.URL.RawQuery = opts.queryParams.Encode()
43+
}
44+
45+
resp, err := client.Do(req)
46+
if err != nil {
47+
return "", err
48+
}
49+
defer resp.Body.Close()
50+
51+
body, err := io.ReadAll(resp.Body)
52+
if err != nil {
53+
return "", err
54+
}
55+
56+
var formattedJSON bytes.Buffer
57+
err = json.Indent(&formattedJSON, body, "", "\t")
58+
if err != nil {
59+
return "", err
60+
}
61+
return formattedJSON.String(), nil
62+
}

0 commit comments

Comments
 (0)