Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes NewClient definition and removes bloated interface #32

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,40 @@ 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).

## [v0.3.0] - 2023-10-26

### Changed

- The interface `Client` is removed and the output of the `NewClient` is the pointer to the struct now. It will facilitate stub of the SDK client.
- [**BREAKING**] `NewClient` requires `Config` to initialise a Neon client. It's meant to improve security by eliminating
support of environment variables for authentication by default. It also simplifies the codebase.

**Example**
```go
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()
if err != nil {
panic(err)
}

log.Printf("%d projects found", len(v.Projects))
}
```
- [**BREAKING**] Removed support of variadic functions used to configure SDK client.

## [v0.2.5] - 2023-10-22

The release incorporates the up-to-date [API contract](openAPIDefinition.json) as of 2023-10-11 00:08:16 GMT.
Expand Down
29 changes: 11 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,9 @@ Where `{{.Ver}}` is the release version.

### Code Snippets

#### Authentication
### Default HTTP Client

Authentication with the Neon Platform is implemented
using [variadic functions](https://gobyexample.com/variadic-functions) and environment variables evaluation in the
following order:

1. Variadic function client's argument;
2. Environment variable `NEON_API_KEY`.

Note that if the API key is provided as the variadic function argument, key from the environment variable `NEON_API_KEY`
will be ignored.

##### Variadic Function
The following snippet demonstrates how to initialize SDK which will use default HTTP client.

```go
package main
Expand All @@ -75,7 +65,7 @@ import (
)

func main() {
client, err := neon.NewClient(neon.WithAPIKey("{{.NeonApiKey}}"))
client, err := neon.NewClient(neon.Config{Key: "{{.NeonApiKey}}"})
if err != nil {
panic(err)
}
Expand All @@ -89,22 +79,25 @@ func main() {
}
```

##### Environment Variables Evaluation
### Custom HTTP client

**_Requirement_**: a valid Neon [API key](https://neon.tech/docs/manage/api-keys/) must be exported as the environment
variable `NEON_API_KEY`.
The SDK can initialized with a custom HTTP client.

```go
package main

import (
"net/http"
"log"
"time"

neon "github.com/kislerdm/neon-sdk-go"
)

func main() {
client, err := neon.NewClient()
myHTTPClient := &http.Client{Timeout: 30 * time.Second}

client, err := neon.NewClient(neon.Config{Key: "{{.NeonApiKey}}", HTTPClient: myHTTPClient})
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -132,7 +125,7 @@ import (
)

func main() {
client, err := neon.NewClient(neon.WithHTTPClient(neon.NewMockHTTPClient()))
client, err := neon.NewClient(neon.Config{HTTPClient: neon.NewMockHTTPClient()})
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ package sdk_test

import (
"fmt"
"os"
"testing"
"time"

sdk "github.com/kislerdm/neon-sdk-go"
)

func TestSmoke(t *testing.T) {
cl, err := sdk.NewClient()
cl, err := sdk.NewClient(sdk.Config{Key: os.Getenv("NEON_API_KEY")})
if err != nil {
t.Fatalf("cannot initialise SDK: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Find more about the service: https://neon.tech/docs/introduction.

Author: dkisler.com
Author: Dmitry Kisler <https://www.dkisler.com>

*/

Expand Down
60 changes: 60 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package sdk

import (
"bytes"
"encoding/json"
"io"
"net/http"
"strconv"
)

// Error API error.
type Error struct {
HTTPCode int
errorResp
}

func (e Error) Error() string {
return "[HTTP Code: " + strconv.Itoa(e.HTTPCode) + "][Error Code: " + e.Code + "] " + e.Message
}

func (e Error) httpResp() *http.Response {
o, _ := json.Marshal(e.errorResp)
return &http.Response{
Status: e.Code,
StatusCode: e.HTTPCode,
Body: io.NopCloser(bytes.NewReader(o)),
ContentLength: int64(len(o)),
}
}

type errorResp struct {
Code string `json:"code"`
Message string `json:"message"`
}

func convertErrorResponse(res *http.Response) error {
var v errorResp
buf, err := io.ReadAll(res.Body)
defer func() { _ = res.Body.Close() }()
if err != nil {
return Error{
HTTPCode: res.StatusCode,
errorResp: errorResp{
Message: "cannot read response bytes",
},
}
}
if err := json.Unmarshal(buf, &v); err != nil {
return Error{
HTTPCode: res.StatusCode,
errorResp: errorResp{
Message: err.Error(),
},
}
}
return Error{
HTTPCode: res.StatusCode,
errorResp: v,
}
}
170 changes: 0 additions & 170 deletions generator/extractSpecs_internal_test.go

This file was deleted.

Loading
Loading