Skip to content

Commit

Permalink
Feature/39 multiple sources (#40)
Browse files Browse the repository at this point in the history
* Update client for multiple sources

* Bump version
  • Loading branch information
rikbw authored Apr 6, 2021
1 parent c13e2a5 commit 285bcda
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 25 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ This Go module provides a client for connector interactions with an Elimity Insi
package main

import (
"github.com/elimity-com/insights-client-go/v4"
"github.com/elimity-com/insights-client-go/v5"
"time"
)

func main() {
client, err := insights.NewClient("https://local.elimity.com:8081/api", "token")
sourceID := 4
client, err := insights.NewClient("https://local.elimity.com:8081/api", "token", sourceID)
if err != nil {
panic(err)
}
Expand All @@ -34,7 +35,7 @@ func main() {
## Installation

```sh
$ go get github.com/elimity-com/insights-client-go/v4
$ go get github.com/elimity-com/insights-client-go/v5
```

## Compatibility
Expand All @@ -43,4 +44,5 @@ $ go get github.com/elimity-com/insights-client-go/v4
| -------------- | ---------------- |
| 1 | 2.7 - 2.11 |
| 2 | 2.12 - 3.0 |
| 3 | ^3.1 |
| 3 | 3.1 - 3.5 |
| 4 | ^3.6 |
15 changes: 9 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ import (
"fmt"
"io"
"net/http"
"strconv"
)

// Client represents an authenticated HTTP client for an Elimity Insights server.
type Client struct {
basePath string
client *http.Client
token string
sourceID int
}

// NewClient creates a new client that is authenticated with the given token at a server at the given base path.
func NewClient(basePath, token string) (Client, error) {
func NewClient(basePath, token string, sourceID int) (Client, error) {
client := Client{
basePath: basePath,
client: http.DefaultClient,
token: token,
sourceID: sourceID,
}
return client, nil
}
Expand All @@ -28,14 +31,15 @@ func NewClient(basePath, token string) (Client, error) {
// server at the given base path.
//
// The resulting client does not verify the TLS certificate of the configured server.
func NewClientDisableTLSCertificateVerification(basePath, token string) Client {
func NewClientDisableTLSCertificateVerification(basePath, token string, sourceID int) Client {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client := &http.Client{Transport: transport}
return Client{
basePath: basePath,
client: client,
token: token,
sourceID: sourceID,
}
}

Expand All @@ -45,10 +49,9 @@ func (c Client) performRequest(path, requestContentType string, requestBody io.R
if err != nil {
panic(err)
}
header := request.Header
authorization := fmt.Sprintf("Bearer %s", c.token)
header.Set("Authorization", authorization)
header.Set("Content-Type", requestContentType)
request.Header.Set("Content-Type", requestContentType)
request.SetBasicAuth(strconv.Itoa(c.sourceID), c.token)

response, err := c.client.Do(request)
if err != nil {
return fmt.Errorf("failed performing request: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import (
"net/http/httptest"
"testing"

"github.com/elimity-com/insights-client-go/v4"
"github.com/elimity-com/insights-client-go/v5"
)

func TestNewClientDisableTLSCertificateVerification(t *testing.T) {
fun := http.HandlerFunc(handler)
server := httptest.NewTLSServer(fun)
client := insights.NewClientDisableTLSCertificateVerification(server.URL, "foo")
client := insights.NewClientDisableTLSCertificateVerification(server.URL, "foo", 0)
if err := client.Infof("foo"); err != nil {
t.Fatalf("failed creating info log: %v", err)
}
}

func handler(http.ResponseWriter, *http.Request) {}

func setup(t *testing.T, handler http.Handler) (insights.Client, *httptest.Server) {
func setup(t *testing.T, handler http.Handler, sourceID int) (insights.Client, *httptest.Server) {
server := httptest.NewServer(handler)
client, err := insights.NewClient(server.URL, "foo")
client, err := insights.NewClient(server.URL, "foo", sourceID)
if err != nil {
t.Fatalf("failed creating client: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion connectorlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func (c Client) CreateConnectorLogs(logs []ConnectorLog) error {
panic(err)
}
requestBody := bytes.NewReader(requestBodyBytes)
return c.performRequest("custom-connector-logs", "application/json", requestBody)
path := fmt.Sprintf("custom-sources/%d/connector-logs", c.sourceID)
return c.performRequest(path, "application/json", requestBody)
}

// Infof sends an info log to the given client's configured server. The log's message is constructed by formatting
Expand Down
11 changes: 7 additions & 4 deletions connectorlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ package insights_test

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"
"time"

"github.com/elimity-com/insights-client-go/v4"
"github.com/elimity-com/insights-client-go/v5"
)

func TestClientCreateConnectorLogs(t *testing.T) {
sourceID := 4
timestamp := time.Now()
var fun http.HandlerFunc = func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != "/custom-connector-logs" {
t.Fatalf(`got path %q, want "/custom-connector-logs"`, request.URL.Path)
path := fmt.Sprintf("/custom-sources/%d/connector-logs", sourceID)
if request.URL.Path != path {
t.Fatalf(`got path %q, want %s`, request.URL.Path, path)
}

bs, err := ioutil.ReadAll(request.Body)
Expand Down Expand Up @@ -51,7 +54,7 @@ func TestClientCreateConnectorLogs(t *testing.T) {
}
}

client, server := setup(t, fun)
client, server := setup(t, fun, sourceID)
defer server.Close()

logs := []insights.ConnectorLog{
Expand Down
4 changes: 3 additions & 1 deletion domaingraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"compress/zlib"
"encoding/json"
"fmt"
tim "time"
)

Expand Down Expand Up @@ -36,7 +37,8 @@ func (c Client) ReloadDomainGraph(domainGraph DomainGraph) error {
if err := writer.Close(); err != nil {
panic(err)
}
return c.performRequest("custom-connector-domain-graphs", "application/octet-stream", requestBody)
path := fmt.Sprintf("custom-sources/%d/snapshots", c.sourceID)
return c.performRequest(path, "application/octet-stream", requestBody)
}

// DomainGraph represents a graph of domain data that may be managed at an Elimity Insights server.
Expand Down
11 changes: 7 additions & 4 deletions domaingraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package insights_test
import (
"compress/zlib"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/elimity-com/insights-client-go/v4"
"github.com/elimity-com/insights-client-go/v5"
"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -167,9 +168,11 @@ func TestClientReloadDomainGraphTimestamp(t *testing.T) {
}

func domainGraphTestClientServer(t *testing.T, expectedBodyString string) (insights.Client, *httptest.Server) {
sourceID := 5
var handlerFunc http.HandlerFunc = func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != "/custom-connector-domain-graphs" {
t.Fatalf(`got path %q, want "/custom-connector-domain-graphs"`, request.URL.Path)
path := fmt.Sprintf("/custom-sources/%d/snapshots", sourceID)
if request.URL.Path != path {
t.Fatalf(`got path %q, want %s`, request.URL.Path, path)
}

reader, err := zlib.NewReader(request.Body)
Expand Down Expand Up @@ -203,5 +206,5 @@ func domainGraphTestClientServer(t *testing.T, expectedBodyString string) (insig
}
}

return setup(t, handlerFunc)
return setup(t, handlerFunc, sourceID)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/elimity-com/insights-client-go/v4
module github.com/elimity-com/insights-client-go/v5

go 1.13

Expand Down

0 comments on commit 285bcda

Please sign in to comment.