Skip to content

Commit

Permalink
docs: add tested and running go sdk examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Jun 16, 2021
1 parent cac507e commit 3b56bb5
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 5 deletions.
1 change: 0 additions & 1 deletion examples/go/.replit

This file was deleted.

3 changes: 2 additions & 1 deletion examples/go/init/login/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
ory "github.com/ory/kratos-client-go"
"log"

ory "github.com/ory/kratos-client-go"
)

func sdk(endpoint string) *ory.APIClient {
Expand Down
12 changes: 12 additions & 0 deletions examples/go/init/login/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestLogin(t *testing.T) {
flow := initLogin()
require.NotEmpty(t, flow.Id)
}
3 changes: 2 additions & 1 deletion examples/go/init/recovery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
ory "github.com/ory/kratos-client-go"
"log"

ory "github.com/ory/kratos-client-go"
)

func sdk(endpoint string) *ory.APIClient {
Expand Down
12 changes: 12 additions & 0 deletions examples/go/init/recovery/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestRecovery(t *testing.T) {
flow := initRecovery()
require.NotEmpty(t, flow.Id)
}
3 changes: 2 additions & 1 deletion examples/go/init/registration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
ory "github.com/ory/kratos-client-go"
"log"

ory "github.com/ory/kratos-client-go"
)

func sdk(endpoint string) *ory.APIClient {
Expand Down
12 changes: 12 additions & 0 deletions examples/go/init/registration/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestRegistration(t *testing.T) {
flow := initRegistration()
require.NotEmpty(t, flow.Id)
}
63 changes: 63 additions & 0 deletions examples/go/init/settings/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"

"github.com/google/uuid"

ory "github.com/ory/kratos-client-go"
)

func sdk(endpoint string) *ory.APIClient {
conf := ory.NewConfiguration()
conf.Servers = ory.ServerConfigurations{{URL: endpoint}}
return ory.NewAPIClient(conf)
}

// If you use Open Source this would be:
//
// var c = sdk("https://127.0.0.1:4433")
var c = sdk("https://playground.projects.oryapis.com/api/kratos/public")
var ctx = context.Background()

func initSettings() *ory.SettingsFlow {
// Create a temporary user
sessionToken := createUserAndGetSession()
flow, _, err := c.PublicApi.InitializeSelfServiceSettingsWithoutBrowserExecute(ory.PublicApiApiInitializeSelfServiceSettingsWithoutBrowserRequest{}.
XSessionToken(sessionToken))
if err != nil {
log.Fatalf("An error ocurred: %s\n", err)
}

return flow
}

func main() {
flow := initSettings()
out, _ := json.MarshalIndent(flow, "", " ")
fmt.Println(string(out))
}

// createUserAndGetSession creates a user that we use for in settings flow.
func createUserAndGetSession() string {
flow, _, err := c.PublicApi.InitializeSelfServiceRegistrationWithoutBrowser(ctx).Execute()
if err != nil {
log.Fatalf("An error ocurred: %s\n", err)
}

result, _, err := c.PublicApi.SubmitSelfServiceRegistrationFlow(ctx).Flow(flow.Id).SubmitSelfServiceRegistrationFlow(ory.SubmitSelfServiceRegistrationFlow{
&ory.SubmitSelfServiceRegistrationFlowWithPasswordMethod{
Method: "password",
Password: ory.PtrString(uuid.New().String()),
Traits: map[string]interface{}{"email": "dev+" + uuid.New().String() + "@ory.sh"},
},
}).Execute()
if err != nil {
log.Fatalf("An error ocurred: %s\n", err)
}

return *result.SessionToken
}
12 changes: 12 additions & 0 deletions examples/go/init/settings/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestSettings(t *testing.T) {
flow := initSettings()
require.NotEmpty(t, flow.Id)
}
3 changes: 2 additions & 1 deletion examples/go/init/verification/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
ory "github.com/ory/kratos-client-go"
"log"

ory "github.com/ory/kratos-client-go"
)

func sdk(endpoint string) *ory.APIClient {
Expand Down
12 changes: 12 additions & 0 deletions examples/go/init/verification/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestVerification(t *testing.T) {
flow := initVerification()
require.NotEmpty(t, flow.Id)
}

0 comments on commit 3b56bb5

Please sign in to comment.