77 <h4 >The perfect starting point to integrate <a href =" https://openapi.com/ " >Openapi®</a > within your Go project</h4 >
88
99[ ![ Build Status] ( https://github.com/openapi/openapi-go-sdk/actions/workflows/go.yml/badge.svg )] ( https://github.com/openapi/openapi-go-sdk/actions/workflows/go.yml )
10- [ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/openapi-it /openapi-cli-go )] ( https://goreportcard.com/report/github.com/openapi-it /openapi-cli-go )
11- [ ![ Go Reference] ( https://pkg.go.dev/badge/github.com/openapi-it /openapi-cli-go .svg )] ( https://pkg.go.dev/github.com/openapi-it /openapi-cli-go )
10+ [ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/openapi/openapi-go-sdk )] ( https://goreportcard.com/report/github.com/openapi/openapi-go-sdk )
11+ [ ![ Go Reference] ( https://pkg.go.dev/badge/github.com/openapi/openapi-go-sdk .svg )] ( https://pkg.go.dev/github.com/openapi/openapi-go-sdk )
1212[ ![ License] ( https://img.shields.io/github/license/openapi/openapi-go-sdk )] ( LICENSE )
1313<br >
1414[ ![ Linux Foundation Member] ( https://img.shields.io/badge/Linux%20Foundation-Silver%20Member-003778?logo=linux-foundation&logoColor=white )] ( https://www.linuxfoundation.org/about/members )
@@ -44,63 +44,98 @@ For a complete list of all available services, check out the [Openapi Marketplac
4444
4545## Installation
4646
47+ Add the SDK to your project:
48+
4749``` bash
48- go get github.com/openapi-it/openapi-cli-go
50+ go get github.com/openapi/openapi-go-sdk
51+ ```
52+
53+ Then import the client package:
54+
55+ ``` go
56+ import " github.com/openapi/openapi-go-sdk/pkg/client"
4957```
5058
5159## Usage
5260
61+ ### Token generation
62+
63+ Use ` OauthClient ` to authenticate with your credentials and generate a scoped access token.
64+ The ` test ` flag switches between the sandbox (` true ` ) and production (` false ` ) OAuth endpoint.
65+
5366``` go
5467package main
5568
5669import (
57- " bytes"
5870 " context"
5971 " encoding/json"
72+ " fmt"
6073 " log"
6174
62- client " github.com/openapi-it /openapi-cli-go /pkg/client"
75+ " github.com/openapi/openapi-go-sdk /pkg/client"
6376)
6477
6578func main () {
6679 ctx := context.Background ()
6780
68- // Initialize the OAuth client on the sandbox environment
6981 oauthClient := client.NewOauthClient (" <your_username>" , " <your_apikey>" , true )
7082
71- // Create a token for a list of scopes
7283 scopes := []string {
7384 " GET:test.imprese.openapi.it/advance" ,
7485 " POST:test.postontarget.com/fields/country" ,
7586 }
76- ttl := 3600
77- resp , err := oauthClient.CreateToken (ctx, scopes, ttl)
87+ resp , err := oauthClient.CreateToken (ctx, scopes, 3600 )
7888 if err != nil {
7989 log.Fatal (err)
8090 }
8191
82- // Parse the token response
8392 tokenResponse := struct {
8493 Scopes []string ` json:"scopes"`
8594 Token string ` json:"token"`
8695 }{}
87- _ = json.Unmarshal ([]byte (resp), &tokenResponse)
96+ if err := json.Unmarshal ([]byte (resp), &tokenResponse); err != nil {
97+ log.Fatal (err)
98+ }
99+
100+ fmt.Printf (" token: %s \n " , tokenResponse.Token )
101+ }
102+ ```
103+
104+ ### Making API calls
105+
106+ Use ` Client ` with the token obtained above to call any Openapi service.
107+ Pass the base URL and endpoint separately so the client can correctly attach query parameters.
108+
109+ ``` go
110+ package main
88111
89- // Initialize the API client with the token
90- apiClient := client.NewClient (tokenResponse.Token )
112+ import (
113+ " bytes"
114+ " context"
115+ " encoding/json"
116+ " fmt"
117+ " log"
91118
92- // GET request with query params
119+ " github.com/openapi/openapi-go-sdk/pkg/client"
120+ )
121+
122+ func main () {
123+ ctx := context.Background ()
124+ apiClient := client.NewClient (" <your_access_token>" )
125+
126+ // GET with query parameters
93127 params := map [string ]string {
94128 " denominazione" : " altravia" ,
95129 " provincia" : " RM" ,
96130 " codice_ateco" : " 6201" ,
97131 }
98- _ , err = apiClient.Request (ctx, " GET" , " https://test.imprese.openapi.it" , " /advance" , nil , params)
132+ result , err : = apiClient.Request (ctx, " GET" , " https://test.imprese.openapi.it" , " /advance" , nil , params)
99133 if err != nil {
100134 log.Fatal (err)
101135 }
136+ fmt.Println (result)
102137
103- // POST request with a payload
138+ // POST with a JSON payload
104139 payload := struct {
105140 Limit int ` json:"limit"`
106141 Query struct {
@@ -116,19 +151,38 @@ func main() {
116151 if err := json.NewEncoder (&buf).Encode (payload); err != nil {
117152 log.Fatal (err)
118153 }
119- _, err = apiClient.Request (ctx, " POST" , " https://test.postontarget.com" , " /fields/country" , &buf, nil )
120- if err != nil {
121- log.Fatal (err)
122- }
123-
124- // Delete the token when done
125- _, err = oauthClient.DeleteToken (ctx, tokenResponse.Token )
154+ result, err = apiClient.Request (ctx, " POST" , " https://test.postontarget.com" , " /fields/country" , &buf, nil )
126155 if err != nil {
127156 log.Fatal (err)
128157 }
158+ fmt.Println (result)
129159}
130160```
131161
162+ More complete examples are available in the [ ` examples/ ` ] ( examples/ ) directory.
163+
164+ ## Testing
165+
166+ The SDK ships with a suite of unit tests that use ` net/http/httptest ` — no real network calls, no credentials needed.
167+
168+ Run the full suite:
169+
170+ ``` bash
171+ go test ./...
172+ ```
173+
174+ Run with verbose output to see each test case:
175+
176+ ``` bash
177+ go test -v ./pkg/client/...
178+ ```
179+
180+ Run a single test by name:
181+
182+ ``` bash
183+ go test -v -run TestCreateToken ./pkg/client/...
184+ ```
185+
132186## Contributing
133187
134188Contributions are always welcome! Whether you want to report bugs, suggest new features, improve documentation, or contribute code, your help is appreciated.
@@ -139,7 +193,7 @@ See [docs/contributing.md](docs/contributing.md) for detailed instructions on ho
139193
140194Meet the project authors:
141195
142- - L. Paderi ([ @lpaderiAltravia ] ( https://www.github.com/lpaderiAltravia ) )
196+ - Michael Cuffaro ([ @maiku1008 ] ( https://www.github.com/maiku1008 ) )
143197- Openapi Team ([ @openapi-it ] ( https://github.com/openapi-it ) )
144198
145199## Partners
0 commit comments