Skip to content

Commit

Permalink
docs: cleanup Go snippets (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris authored Jan 8, 2024
1 parent 269bb7c commit e4c4fd6
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 57 deletions.
7 changes: 4 additions & 3 deletions docs/content/getting-started/create-store.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,18 @@ const { id: storeId } = await openFga.createStore({

```go
import (
openfga "github.com/openfga/go-sdk"
. "github.com/openfga/go-sdk/client"
"context"
"os"

. "github.com/openfga/go-sdk/client"
)

func main() {
fgaClient, err := NewSdkClient(&ClientConfiguration{
ApiScheme: os.Getenv("FGA_API_SCHEME"), // optional. Can be "http" or "https". Defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // optional, not needed for \`CreateStore\` and \`ListStores\`, required before calling for all other methods
AuthorizationModelId: openfga.PtrString(os.Getenv("FGA_MODEL_ID")), // optional, can be overridden per request
AuthorizationModelId: os.Getenv("FGA_MODEL_ID"), // optional, can be overridden per request
})

if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions docs/content/getting-started/framework.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ import (

jwtware "github.com/gofiber/jwt/v3"
"github.com/golang-jwt/jwt/v4"
openfga "github.com/openfga/go-sdk"
. "github.com/openfga/go-sdk/client"
)

Expand Down Expand Up @@ -483,7 +482,7 @@ func checkAuthorization(c *fiber.Ctx) error {
ApiScheme: os.Getenv("FGA_API_SCHEME"), // optional. Can be "http" or "https". Defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // optional, not needed for \`CreateStore\` and \`ListStores\`, required before calling for all other methods
AuthorizationModelId: openfga.PtrString(os.Getenv("FGA_MODEL_ID")), // optional, can be overridden per request
AuthorizationModelId: os.Getenv("FGA_MODEL_ID"), // optional, can be overridden per request
})

if err != nil {
Expand All @@ -495,7 +494,7 @@ func checkAuthorization(c *fiber.Ctx) error {
Relation: c.Locals("relation").(string),
Object: c.Locals("object").(string),
}
data, err := fgaClient.OpenFga.Check(context.Background()).Body(body).Execute()
data, err := fgaClient.Check(context.Background()).Body(body).Execute()

if err != nil {
return fiber.NewError(fiber.StatusServiceUnavailable, "Unable to check for authorization")
Expand Down
27 changes: 12 additions & 15 deletions docs/content/getting-started/setup-sdk-client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ const openFga = new OpenFgaClient({
import (
"os"

openfga "github.com/openfga/go-sdk"
. "github.com/openfga/go-sdk/client"
)

func main() {
fgaClient, err := NewSdkClient(&ClientConfiguration{
ApiScheme: os.Getenv("FGA_API_SCHEME"), // optional. Can be "http" or "https". Defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // optional, not needed for \`CreateStore\` and \`ListStores\`, required before calling for all other methods
AuthorizationModelId: openfga.PtrString(os.Getenv("FGA_MODEL_ID")), // optional, can be overridden per request
ApiScheme: os.Getenv("FGA_API_SCHEME"), // optional. Can be "http" or "https". Defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // optional, not needed for \`CreateStore\` and \`ListStores\`, required before calling for all other methods
AuthorizationModelId: os.Getenv("FGA_MODEL_ID"), // optional, can be overridden per request
})

if err != nil {
Expand Down Expand Up @@ -158,17 +157,16 @@ const openFga = new OpenFgaClient({
import (
"os"

openfga "github.com/openfga/go-sdk"
. "github.com/openfga/go-sdk/client"
"github.com/openfga/go-sdk/credentials"
)

func main() {
fgaClient, err := NewSdkClient(&ClientConfiguration{
ApiScheme: os.Getenv("FGA_API_SCHEME"), // optional. Can be "http" or "https". Defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // optional, not needed for \`CreateStore\` and \`ListStores\`, required before calling for all other methods
AuthorizationModelId: openfga.PtrString(os.Getenv("FGA_MODEL_ID")), // optional, can be overridden per request
ApiScheme: os.Getenv("FGA_API_SCHEME"), // optional. Can be "http" or "https". Defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // optional, not needed for \`CreateStore\` and \`ListStores\`, required before calling for all other methods
AuthorizationModelId: os.Getenv("FGA_MODEL_ID"), // optional, can be overridden per request
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodApiToken,
Config: &credentials.Config{
Expand Down Expand Up @@ -286,17 +284,16 @@ const openFga = new OpenFgaClient({
import (
"os"

openfga "github.com/openfga/go-sdk"
. "github.com/openfga/go-sdk/client"
"github.com/openfga/go-sdk/credentials"
)

func main() {
fgaClient, err := NewSdkClient(&ClientConfiguration{
ApiScheme: os.Getenv("FGA_API_SCHEME"), // optional. Can be "http" or "https". Defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // optional, not needed for \`CreateStore\` and \`ListStores\`, required before calling for all other methods
AuthorizationModelId: openfga.PtrString(os.Getenv("FGA_MODEL_ID")), // optional, can be overridden per request
ApiScheme: os.Getenv("FGA_API_SCHEME"), // optional. Can be "http" or "https". Defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // optional, not needed for \`CreateStore\` and \`ListStores\`, required before calling for all other methods
AuthorizationModelId: os.Getenv("FGA_MODEL_ID"), // optional, can be overridden per request
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodClientCredentials,
Config: &credentials.Config{
Expand Down
3 changes: 2 additions & 1 deletion docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ const config = {
setupNote: `// ApiTokenIssuer, ApiAudience, ClientId and ClientSecret are optional.\n`,
},
go: {
importStatement: `. "github.com/openfga/go-sdk/client"`,
importStatement: `. "github.com/openfga/go-sdk"
. "github.com/openfga/go-sdk/client"`,
apiName: `OpenFgaClient`,
setupNote: `// ApiTokenIssuer, ApiAudience, ClientId and ClientSecret are optional.\n`,
},
Expand Down
2 changes: 1 addition & 1 deletion src/components/Docs/SnippetViewer/CheckRequestViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const { allowed } = await fgaClient.check({
/* eslint-disable no-tabs */
return `
options := ClientCheckOptions{
AuthorizationModelId: "${modelId}",
AuthorizationModelId: PtrString("${modelId}"),
}
body := ClientCheckRequest{
Expand Down
7 changes: 5 additions & 2 deletions src/components/Docs/SnippetViewer/ExpandRequestViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ const { tree } = await fgaClient.expand({
/* eslint-disable no-tabs */
return `
options := ClientExpandOptions{
AuthorizationModelId: openfga.PtrString("${modelId}"),
AuthorizationModelId: PtrString("${modelId}"),
}
body := ClientExpandRequest{
Relation: "${opts.relation}", // expand all who has "${opts.relation}" relation
Object: "${opts.object}", // with the object "${opts.object}"
}
data, err := fgaClient.Expand(context.Background()).Body(requestBody).Options(options).Execute()
data, err := fgaClient.Expand(context.Background()).
Body(body).
Options(options).
Execute()
// data = { tree: ...}`;

Expand Down
11 changes: 5 additions & 6 deletions src/components/Docs/SnippetViewer/ListObjectsRequestViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ function listObjectsRequestViewer(lang: SupportedLanguage, opts: ListObjectsRequ
// response.objects = [${expectedResults.map((r) => `"${r}"`).join(', ')}]`;
case SupportedLanguage.GO_SDK:
/* eslint-disable no-tabs */
return `
options := ClientListObjectsOptions{
AuthorizationModelId: openfga.PtrString("${modelId}"),
return `options := ClientListObjectsOptions{
AuthorizationModelId: PtrString("${modelId}"),
}
body := ClientListObjectsRequest{
Expand Down Expand Up @@ -134,9 +133,9 @@ ${
}
data, err := fgaClient.ListObjects(context.Background()).
Body(requestBody).
Options(options).
Execute()
Body(body).
Options(options).
Execute()
// data = { "objects": [${expectedResults.map((r) => `"${r}"`).join(', ')}] }`;
case SupportedLanguage.DOTNET_SDK:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ await fgaClient.readChanges({ type }, { pageSize, continuationToken });`;
case SupportedLanguage.GO_SDK: {
// eslint-disable-next-line max-len
return `options := ClientReadChangesOptions{${
opts.pageSize ? `\n\tPageSize: openfga.PtrInt32(${opts.pageSize}),\n` : ''
}${opts.continuationToken ? `\n\tContinuationToken: openfga.PtrString("${opts.continuationToken}"),\n` : ''}}
opts.pageSize ? `\n\tPageSize: PtrInt32(${opts.pageSize}),\n` : ''
}${opts.continuationToken ? `\n\tContinuationToken: PtrString("${opts.continuationToken}"),\n` : ''}}
body := ClientReadChangesRequest{${opts.type ? `\n\tType: "${opts.type}",` : ''}
}
data, err := fgaClient.ReadChanges(context.Background()).Body(body).Options(options).Execute()
data, err := fgaClient.ReadChanges(context.Background()).
Body(body).
Options(options).
Execute()
if err != nil {
// .. Handle error
Expand Down
11 changes: 7 additions & 4 deletions src/components/Docs/SnippetViewer/ReadRequestViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ const { tuples } = await fgaClient.read({

case SupportedLanguage.GO_SDK: {
const requestTuples = opts.object
? (opts.user ? `\tUser: openfga.PtrString("${opts.user}"),\n` : '') +
(opts.relation ? `\tRelation: openfga.PtrString("${opts.relation}"),\n` : '') +
`\tObject: openfga.PtrString("${opts.object}"),\n`
? (opts.user ? `\tUser: PtrString("${opts.user}"),\n` : '') +
(opts.relation ? `\tRelation: PtrString("${opts.relation}"),\n` : '') +
`\tObject: PtrString("${opts.object}"),\n`
: '';

/* eslint-disable no-tabs */
Expand All @@ -84,7 +84,10 @@ body := ClientReadRequest{
${requestTuples}
}
data, err := fgaClient.Read(context.Background()).Body(requestBody).Options(options).Execute()
data, err := fgaClient.Read(context.Background()).
Body(body).
Options(options).
Execute()
// data = { "tuples": [${readTuples}] }`;
}
Expand Down
5 changes: 3 additions & 2 deletions src/components/Docs/SnippetViewer/SdkSetup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ const fgaClient = new ${languageMappings['js'].apiName}({
case SupportedLanguage.GO_SDK:
/* eslint-disable no-tabs */
return `import (
${importSdkStatement(lang, languageMappings)}
"os"
${importSdkStatement(lang, languageMappings)}
)
func main() {
Expand All @@ -61,7 +62,7 @@ func main() {
ApiScheme: os.Getenv("FGA_SCHEME"), // Either "http" or "https", defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // optional, not needed for \`CreateStore\` and \`ListStores\`, required before calling for all other methods
AuthorizationModelId: openfga.PtrString(os.Getenv("FGA_MODEL_ID")), // Optional, can be overridden per request
AuthorizationModelId: os.Getenv("FGA_MODEL_ID"), // Optional, can be overridden per request
})
if err != nil {
Expand Down
18 changes: 6 additions & 12 deletions src/components/Docs/SnippetViewer/WriteAuthzModelViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getFilteredAllowedLangs, SupportedLanguage, LanguageMappings } from './SupportedLanguage';
import { getFilteredAllowedLangs, SupportedLanguage } from './SupportedLanguage';
import { defaultOperationsViewer } from './DefaultTabbedViewer';
import assertNever from 'assert-never/index';
import { AuthorizationModel } from '@openfga/sdk';
Expand Down Expand Up @@ -30,27 +30,25 @@ const { authorization_model_id: id } = await fgaClient.writeAuthorizationModel($
// id = "1uHxCSuTP0VKPYSnkq1pbb1jeZw"`;
}

function writeAuthZModelViewerGo(authorizationModel: AuthorizationModel, apiName: string): string {
function writeAuthZModelViewerGo(authorizationModel: AuthorizationModel): string {
/* eslint-disable no-tabs */
return `
var writeAuthorizationModelRequestString = ${JSON.stringify(JSON.stringify(authorizationModel))}
var body WriteAuthorizationModelRequest
if err := json.Unmarshal([]byte(writeAuthorizationModelRequestString), &body); err != nil {
t.Errorf("%v", err)
// .. Handle error
return
}
data, err := fgaClient.${apiName}.WriteAuthorizationModel(context.Background()).
data, err := fgaClient.WriteAuthorizationModel(context.Background()).
Body(body).
Execute()
if err != nil {
// .. Handle error
}
// data.AuthorizationModelId = "1uHxCSuTP0VKPYSnkq1pbb1jeZw"
`;
// data.AuthorizationModelId = "1uHxCSuTP0VKPYSnkq1pbb1jeZw"`;
}

function writeAuthZModelViewerDotnet(authorizationModel: AuthorizationModel): string {
Expand All @@ -73,11 +71,7 @@ async def write_authorization_model():
`;
}

function writeAuthZModelViewer(
lang: SupportedLanguage,
opts: WriteAuthzModelViewerOpts,
languageMappings: LanguageMappings,
) {
function writeAuthZModelViewer(lang: SupportedLanguage, opts: WriteAuthzModelViewerOpts) {
switch (lang) {
case SupportedLanguage.CLI: {
return writeAuthZModelViewerCli(opts.authorizationModel);
Expand All @@ -92,7 +86,7 @@ function writeAuthZModelViewer(
}

case SupportedLanguage.GO_SDK: {
return writeAuthZModelViewerGo(opts.authorizationModel, languageMappings['go'].apiName);
return writeAuthZModelViewerGo(opts.authorizationModel);
}

case SupportedLanguage.DOTNET_SDK: {
Expand Down
9 changes: 4 additions & 5 deletions src/components/Docs/SnippetViewer/WriteRequestViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ await fgaClient.write({
.join('')
: '';

const writes = ` Writes: []ClientTupleKey{${
const writes = `\n Writes: []ClientTupleKey{${
writeTuples.length > 0
? `\n${writeTuples}
},`
Expand All @@ -161,15 +161,14 @@ await fgaClient.write({

return `
options := ClientWriteOptions{
AuthorizationModelId: "${modelId}",
AuthorizationModelId: PtrString("${modelId}"),
}
body := fgaClient.ClientWriteRequest{
${opts.relationshipTuples ? writes : ''}${opts.deleteRelationshipTuples ? deletes : ''}
body := ClientWriteRequest{${opts.relationshipTuples ? writes : ''}${opts.deleteRelationshipTuples ? deletes : ''}
}
data, err := fgaClient.Write(context.Background()).
Body(requestBody).
Body(body).
Options(options).
Execute()
Expand Down

0 comments on commit e4c4fd6

Please sign in to comment.