Skip to content

Commit

Permalink
Merge pull request #15 from jzelinskie/multiclients
Browse files Browse the repository at this point in the history
reorganize package to support multiple clients
  • Loading branch information
jzelinskie authored Jul 6, 2021
2 parents 46ed8a2 + 5ffbfb0 commit 47a8c1a
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 164 deletions.
24 changes: 8 additions & 16 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,19 @@ jobs:
- uses: "actions/setup-go@v2"
with:
go-version: ^1.16
- name: "go fmt"
- name: "go imports"
run: |
GOFMT_OUTPUT="$(gofmt -l `find ./ -name '*.go' | grep -v vendor` 2>&1)"
if [ -n "$GOFMT_OUTPUT" ]; then
go install mvdan.cc/gofumpt/gofumports@latest
GOFUMPT_OUTPUT="$(gofumports -d `find . -name '*.go' | grep -v ./proto/`)"
if [ -n "$GOFUMPT_OUTPUT" ]; then
echo "All the following files are not correctly formatted"
echo "${GOFMT_OUTPUT}"
echo "${GOFUMPT_OUTPUT}"
exit 1
fi
- name: "go imports"
- name: "go lint"
run: |
exec 5>&1
GOIMPORTS_OUTPUT="$(goimports -l -w `find ./ -name '*.go' | grep -v vendor` | tee /dev/fd/5)"
if [ -n "$GOIMPORTS_OUTPUT" ]; then
echo "All following has imports not properly ordered"
echo "${GOIMPORTS_OUTPUT}"
exit 1
fi
go install github.com/mgechev/revive@latest
revive ./...
- name: "go mod tidy"
run: |
GO_MOD_OUPUT="$(go mod tidy -v 2>&1)"
Expand All @@ -44,7 +37,6 @@ jobs:
echo "${GO_MOD_OUPUT}"
exit 1
fi
- uses: golangci/golangci-lint-action@v2
test:
name: "Test"
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# authzed-go

The official Go client library for Authzed.
This repository is a collection of Go packages specialized for each versions of the Authzed API.

## Example
## Example (v0 API)

This example demonstrates initializing a client and making a [Check request] to an existing [Namespace].

A more full example can be found in the [examples directory].
A more full example can be found in [v0/examples].

[Check request]:https://docs.authzed.com/concept/check
[Namespace]: https://docs.authzed.com/concept/namespaces
[examples directory]: examples
[v0/examples]: /v0/examples

```go
package main
Expand All @@ -19,7 +20,7 @@ import (
"context"
"log"

"github.com/authzed/authzed-go"
"github.com/authzed/authzed-go/v0"
"github.com/authzed/authzed-go/proto/authzed/api/v0"
)

Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ module github.com/authzed/authzed-go
go 1.16

require (
github.com/authzed/grpcutil v0.0.0-20210702215403-5fe6e00b4101
github.com/envoyproxy/protoc-gen-validate v0.6.1
github.com/golang/protobuf v1.5.2
github.com/jzelinskie/stringz v0.0.0-20210414224931-d6a8ce844a70
github.com/processout/grpc-go-pool v1.2.1
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
google.golang.org/grpc v1.35.0
google.golang.org/grpc v1.39.0
google.golang.org/protobuf v1.26.0
)
43 changes: 36 additions & 7 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
5 changes: 3 additions & 2 deletions examples/example.go → v0/examples/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"log"

"github.com/authzed/authzed-go"
v0 "github.com/authzed/authzed-go/proto/authzed/api/v0"
"github.com/authzed/authzed-go/v0"
)

const (
Expand Down Expand Up @@ -36,7 +36,8 @@ func main() {
createTuple(tuple(aDoc("owner"), anOwner)),
createTuple(tuple(aDoc("contributor"), anEditor)),
createTuple(tuple(aDoc("viewer"), aViewer)),
}})
},
})
if err != nil {
log.Fatalf("unable to write tuples: %s", err)
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion examples_test.go → v0/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"log"

"github.com/authzed/authzed-go"
v0 "github.com/authzed/authzed-go/proto/authzed/api/v0"
"github.com/authzed/authzed-go/v0"
)

func ExampleNewClient() {
Expand Down
2 changes: 1 addition & 1 deletion filter.go → v0/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"reflect"

v0 "github.com/authzed/authzed-go/proto/authzed/api/v0"
"github.com/authzed/authzed-go/x/parallel"
"github.com/authzed/authzed-go/v0/x/parallel"
)

const (
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 8 additions & 10 deletions integration_test.go → v0/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"google.golang.org/grpc"

v0 "github.com/authzed/authzed-go/proto/authzed/api/v0"
"github.com/authzed/authzed-go/x/nsbuilder"
"github.com/authzed/authzed-go/v0/x/nsbuilder"
)

var setupOnce sync.Once
Expand All @@ -23,15 +23,13 @@ var (
)
)

var (
fred = &v0.User{UserOneof: &v0.User_Userset{
Userset: &v0.ObjectAndRelation{
Namespace: "test/user",
ObjectId: "fred",
Relation: "...",
},
}}
)
var fred = &v0.User{UserOneof: &v0.User_Userset{
Userset: &v0.ObjectAndRelation{
Namespace: "test/user",
ObjectId: "fred",
Relation: "...",
},
}}

func setupTenant(t *testing.T) *Client {
client, err := NewClient("localhost:50051", grpc.WithInsecure())
Expand Down
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions v1alpha1/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package authzed

import (
"github.com/jzelinskie/stringz"
"google.golang.org/grpc"

v1alpha1 "github.com/authzed/authzed-go/proto/authzed/api/v1alpha1"
)

// Client represents an open connection to Authzed.
//
// Clients are backed by a gRPC client and as such are thread-safe.
type Client struct {
v1alpha1.SchemaServiceClient
}

// NewClient initializes a brand new client for interacting with Authzed.
func NewClient(endpoint string, opts ...grpc.DialOption) (*Client, error) {
conn, err := grpc.Dial(
stringz.DefaultEmpty(endpoint, "grpc.authzed.com:443"),
opts...,
)
if err != nil {
return nil, err
}

return &Client{
v1alpha1.NewSchemaServiceClient(conn),
}, nil
}
23 changes: 23 additions & 0 deletions v1alpha1/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package authzed_test

import (
"fmt"
"log"
"testing"

"github.com/authzed/grpcutil"

authzed "github.com/authzed/authzed-go/v1alpha1"
)

func ExampleNewClient(_ *testing.T) {
client, err := authzed.NewClient(
"grpc.authzed.com:443",
grpcutil.WithBearerToken("tc_my_token_deadbeefdeadbeefdeadbeef"),
grpcutil.WithSystemCerts(false),
)
if err != nil {
log.Fatalf("failed to connect to authzed: %s", err)
}
fmt.Println(client)
}
Loading

0 comments on commit 47a8c1a

Please sign in to comment.