ory / sdk

The place where ORY's SDKs are being auto-generated
Apache License 2.0
141 stars 85 forks source link

GO: Error 'Index 0 out of range -1' in kratos-client-go #198

Closed tjarkmeyer closed 2 years ago

tjarkmeyer commented 2 years ago

Preflight checklist

Describe the bug

The following code is simply an example from the docs but it throws the error 'Index 0 out of range -1'. The response is nil.

package main

import (
    "context"
    "fmt"
    "os"

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

func main() {
    apiClient := client.NewAPIClient(&client.Configuration{
        Scheme: "https",
        Host:   "playground.projects.oryapis.com",
        DefaultHeader: map[string]string{
            "Authorization": "Bearer ory_pat_xRKLsFEOUFQFVBjd6o3FQDifaLYhabGd",
        },
    })

    adminCreateIdentityBody := *client.NewAdminCreateIdentityBody(
        "default",
        map[string]interface{}{
            "email": "foo@example.com",
            "name": map[string]string{
                "first": "foo",
                "last":  "bar",
            },
        },
    ) // AdminCreateIdentityBody |  (optional)

    createdIdentity, r, err := apiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute()
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminCreateIdentity``: %v\n", err)
        fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
    }
    fmt.Fprintf(os.Stdout, "Created identity with ID: %v\n", createdIdentity.Id)
}

Reproducing the bug

Run the code from above.

Relevant log output

No response

Relevant configuration

No response

Version

kratos-client-go v0.10.1

On which operating system are you observing this issue?

Linux

In which environment are you deploying?

Ory Cloud

Additional Context

No response

Benehiko commented 2 years ago

Hi @tjarkmeyer

You are missing the Servers key on the struct.

conf := &client.Configuration{
    Servers: []client.ServerConfiguration{
        {
            URL: "<project-url>",
        },
    },
    DefaultHeader: map[string]string{
        "Authorization": "Bearer <pat-token>",
    },
}

The console also does not use the "default" schema ID. You will need to retrieve all of the schemas first to get the unique ID.

vinckr commented 2 years ago

Hello @tjarkmeyer

edit: seems Benehiko was faster 😁

check out this snippet, it should work for Ory Cloud, your example is missing the

Servers: []client.ServerConfiguration{
            {
                URL: "<project-url>",
            },

Also please use the new ory/client-go, we still have to document this and migrate or rather think of a good migration process from the old SDK. I will see to update the SDK snippets in the docs accordingly.

package main

import (
    "context"
    "fmt"
    "github.com/ory/client-go"
)

func main() {
    conf := &client.Configuration{
        Servers: []client.ServerConfiguration{
            {
                URL: "<project-url>",
            },
        },
        DefaultHeader: map[string]string{
            "Authorization": "Bearer <pat-token>",
        },
    }

    api := client.NewAPIClient(conf)

    schemas, _, err := api.V0alpha2Api.ListIdentitySchemas(context.Background()).Execute()
    if err != nil {
        panic(err)
    }

    for _, schema := range schemas {
        fmt.Printf("SchemaID: %s\n", schema.Id)
        fmt.Printf("SchemaName: %s\n", schema.Name)
    }

    user := client.NewAdminCreateIdentityBody("<really-long-id>", map[string]interface{}{
        "email": "a-new-test2-user@example.com",
    })

    identity, _, err := api.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(*user).Execute()
    if err != nil {
        panic(err)
    }
    fmt.Printf("%v+\n", identity)
}
ghost commented 2 years ago

@Benehiko @vinckr Works now! Thanks!