Get List of Projects with Org API Key
Opened this issue · 0 comments
jblackburn21 commented
I'm get a panic when trying to list all projects using an Org API Key. This works with an environment scoped API Key.
package main
import (
"context"
"fmt"
"github.com/permitio/permit-golang/pkg/config"
"github.com/permitio/permit-golang/pkg/permit"
"go.uber.org/zap"
)
func main() {
// This line initializes the SDK and connects your Go app
// to the Permit.io PDP container (you've set in the previous step), with the API key provided.
permitClient := permit.NewPermit(
// Building new config for Permit client
config.NewConfigBuilder(
// your api key "***").
// Set the PDP URL
WithLogger(zap.NewExample()).
WithPdpUrl("http://localhost:7766").
Build(),
)
ctx := context.TODO()
projects, _ := permitClient.Api.Projects.List(ctx, 1, 10)
for i := 0; i < len(projects); i++ {
fmt.Println(projects[i].Name)
}
}
However, I get this panic
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x10453946c]
goroutine 1 [running]:
github.com/permitio/permit-golang/pkg/config.GetApiKeyLevel(...)
/Users/jblackburn/repos/github/permit/permit-golang/pkg/config/utils.go:17
github.com/permitio/permit-golang/pkg/config.PermitContextFactory({0x10464d6c8?, 0x14000112008?}, 0x140000779f0?, {0x0, 0x0}, {0x0, 0x0}, 0x0)
/Users/jblackburn/repos/github/permit/permit-golang/pkg/config/context.go:54 +0x7c
github.com/permitio/permit-golang/pkg/api.(*permitBaseApi).lazyLoadPermitContext(0x1400000c2e8, {0x10464d6c8?, 0x14000112008?}, {0x140000f5b08, 0x1, 0xf00001042fcc78?})
/Users/jblackburn/repos/github/permit/permit-golang/pkg/api/api.go:25 +0x90
github.com/permitio/permit-golang/pkg/api.(*Projects).List(0x1400000c2e8, {0x10464d6c8, 0x14000112008}, 0x1, 0xa)
/Users/jblackburn/repos/github/permit/permit-golang/pkg/api/projects.go:37 +0x70
main.main()
/Users/jblackburn/repos/poc/permit-sdk/main.go:30 +0x150
exit status 2
I was able to work through these panics, but then get this error
{"level":"error","msg":"","error":"ErrorCode: ContextError ErrorType:general_error Message:The context is missing or invalid - You're trying to use an SDK method that's specific to a project,but you haven't set the current project in your client's context yet,or you are using an organization level API key.Please set the context to a specificproject using `PermitClient.SetPermitContext()` method."}