neon-sdk-go
neon-sdk-go copied to clipboard
Go SDK to communicate to Neon SaaS platform
Go SDK for Neon Postgres SaaS Platform
-
How to use
- Prerequisites
- Installation
-
Code Snippets
- Default HTTP Client
- Custom HTTP Client
- Mock
-
Development
- Commands
- Contribution
The SDK to manage Neon Platform programmatically.
Neon is a fully managed serverless PostgreSQL with a generous free tier. Neon separates storage and compute and offers modern developer features such as serverless, branching, bottomless storage, and more. Neon is open source and written in Rust.
Find more about Neon here.
How to use
Prerequisites
Installation
Add the SDK as a module dependency:
go get github.com/kislerdm/neon-sdk-go
Run to specify the release version:
go get github.com/kislerdm/neon-sdk-go@{{.Ver}}
Where {{.Ver}}
is the release version.
Code Snippets
Default HTTP Client
The following snippet demonstrates how to initialize SDK which will use default HTTP client.
package main
import (
"log"
neon "github.com/kislerdm/neon-sdk-go"
)
func main() {
client, err := neon.NewClient(neon.Config{Key: "{{.NeonApiKey}}"})
if err != nil {
panic(err)
}
v, err := client.ListProjects(nil, nil, nil)
if err != nil {
panic(err)
}
log.Printf("%d projects found", len(v.Projects))
}
Custom HTTP client
The SDK can initialized with a custom HTTP client.
package main
import (
"net/http"
"log"
"time"
neon "github.com/kislerdm/neon-sdk-go"
)
func main() {
myHTTPClient := &http.Client{Timeout: 30 * time.Second}
client, err := neon.NewClient(neon.Config{Key: "{{.NeonApiKey}}", HTTPClient: myHTTPClient})
if err != nil {
panic(err)
}
v, err := client.ListProjects(nil, nil, nil)
if err != nil {
panic(err)
}
log.Printf("%d projects found", len(v.Projects))
}
Mock
The SDK provides the http client's mock for unit tests. An example snippet is shown below.
package main
import (
"log"
neon "github.com/kislerdm/neon-sdk-go"
)
func main() {
client, err := neon.NewClient(neon.Config{HTTPClient: neon.NewMockHTTPClient()})
if err != nil {
panic(err)
}
v, err := client.ListProjects(nil, nil, nil)
if err != nil {
panic(err)
}
log.Printf("%d projects found", len(v.Projects))
}
Development
The SDK codebase is generated using the OpenAPI from the API reference page. The generator application codebase can be found here.
Commands
Prerequisites:
- go ~> 1.18
- gnuMake / cmake
Run to see all available commands:
make help
Run to generate the SDK codebase and store it to PWD, given the OpenAPI spec is available in the
file openAPIDefinition.json
:
make generate-sdk
Set the flag SKIP_TEST=1
to generate the codebase without running the unit tests afterward:
SKIP_TEST=1 make generate-sdk
Run to customise the locations:
make generate-sdk PATH_SDK=##/PATH/TO/OUTPUT/SDK/CODE## PATH_SPEC=##/PATH/TO/SPEC.json##
Run to test generated SDK:
make tests
Run to test generated SDK stored to /PATH/TO/OUTPUT/SDK/CODE
:
make tests DIR=/PATH/TO/OUTPUT/SDK/CODE
Run to test the code generator:
make tests DIR=generator
Run to build the code generator:
make build DIR=generator
Contribution
The SDK is distributed under the MIT license, find full list of dependencies' licenses here.
Please feel free to open an issue ticket, or PR to contribute.