|
| 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) |
0 commit comments