<@U04UQ68083H> Show an example of how to check for...
# ory-copilot
c
@chilly-king-10285 Show an example of how to check for permissions in bulk using the Go client
b
A message from kapa.ai
To check permissions in bulk using the Ory Permissions Go SDK, you can use the
batchCheckPermission
method. While the knowledge sources don't provide a direct example of batch permission checking, I can construct one based on the available information about the API and the single permission check example. Here's an example of how to perform batch permission checks using the Go client:
Copy code
go package main  import (  "context"  "fmt"  "os"   ory "<http://github.com/ory/client-go|github.com/ory/client-go>" )  // Use this context to access Ory APIs which require an Ory API Key. var oryAuthedContext = context.WithValue(context.Background(), ory.ContextAccessToken, os.Getenv("ORY_API_KEY"))  func main() {  // Configure the client  configuration := ory.NewConfiguration()  configuration.Servers = []ory.ServerConfiguration{   {    URL: "<https://your-project-slug.projects.oryapis.com>", // Your Ory Network project URL   },  }  client := ory.NewAPIClient(configuration)   // Create batch check request  tuples := []ory.Relationship{   {    Namespace: ory.PtrString("Blog"),    Object:    ory.PtrString("post-1"),    Relation:  ory.PtrString("view"),    SubjectId: ory.PtrString("user-1"),   },   {    Namespace: ory.PtrString("Blog"),    Object:    ory.PtrString("post-2"),    Relation:  ory.PtrString("edit"),    SubjectId: ory.PtrString("user-1"),   },   {    Namespace: ory.PtrString("Blog"),    Object:    ory.PtrString("post-3"),    Relation:  ory.PtrString("view"),    SubjectId: ory.PtrString("user-2"),   },  }   body := ory.BatchCheckPermissionBody{   Tuples: tuples,  }   // Execute batch permission check  result, r, err := client.PermissionApi.BatchCheckPermission(oryAuthedContext).   BatchCheckPermissionBody(body).   Execute()   if err != nil {   fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)   panic("Encountered error: " + err.Error())  }   // Process results  for i, checkResult := range result.Results {   tuple := tuples[i]   if checkResult.Allowed {    fmt.Printf("%s can %s the %s\n", *tuple.SubjectId, *tuple.Relation, *tuple.Object)   } else {    fmt.Printf("%s cannot %s the %s\n", *tuple.SubjectId, *tuple.Relation, *tuple.Object)   }   if checkResult.Error != nil {    fmt.Printf("Error checking permission: %s\n", *checkResult.Error)   }  } }
This example: 1. Sets up the Ory client with your project URL 2. Creates a batch of permission check requests (tuples) 3. Calls the
BatchCheckPermission
method 4. Processes the results, which will contain an array of check results in the same order as the input tuples The batch check API is documented in the [Ory API Reference](https://www.ory.sh/docs/reference/api) and returns a 200 status code with results for each permission check in the batch.
c
oryClient.PermissionsApi undefined (type *“github.com/ory/client-go”.APIClient has no field or method PermissionsApi
@chilly-king-10285 client.PermissionsApi undefined (type *“github.com/ory/client-go”.APIClient has no field or method PermissionsApi
b
A message from kapa.ai
I see the error you're encountering with the Go client. There's a naming discrepancy in the API client. Based on the knowledge sources, the correct API name is
PermissionApi
(singular) rather than
PermissionsApi
(plural). Here's the correction for your code:
Copy code
go // Incorrect: // client.PermissionsApi.BatchCheckPermission(...)  // Correct: client.PermissionApi.BatchCheckPermission(oryAuthedContext).     BatchCheckPermissionBody(body).     Execute()
This is confirmed in the documentation examples where the permission-related API is consistently referred to as
PermissionApi
(singular form):
Copy code
go check, r, err := ory.PermissionApi.CheckPermission(oryAuthedContext).     Namespace(*&namespace).     Object(*&object).     Relation(*&relation).     SubjectId(*&subjectId).Execute()
The [SDK upgrade guide](https://www.ory.sh/docs/guides/upgrade/sdk-v1#permissions--access-control) also shows this naming convention, where permission-related operations are handled by the
PermissionApi
module. Make sure you're using the latest version of the Ory client-go SDK, as there have been some changes in the API structure as noted in the upgrade documentation.
c
@chilly-king-10285 client.PermissionApi undefined (type *“github.com/ory/client-go”.APIClient has no field or method PermissionApi, but does have field PermissionAPI)
b
A message from kapa.ai
I see the issue now. There's a case sensitivity difference in the API client field name. Based on your error message, the correct field name in the Go client is
PermissionAPI
(with uppercase "API") rather than
PermissionApi
. Here's the corrected code snippet:
Copy code
go // Incorrect: // client.PermissionApi.BatchCheckPermission(...)  // Correct: client.PermissionAPI.BatchCheckPermission(oryAuthedContext).     BatchCheckPermissionBody(body).     Execute()
This appears to be a case sensitivity issue in the Go SDK. While the documentation examples show
PermissionApi
with a lowercase "i", the actual implementation in the client uses
PermissionAPI
with an uppercase "API". This kind of inconsistency can sometimes occur between documentation and implementation, especially after SDK updates. The [SDK V1 upgrade guide](https://www.ory.sh/docs/guides/upgrade/sdk-v1) mentions significant changes to naming conventions, which might explain this discrepancy. When working with the Go client, follow the case sensitivity that your IDE or compiler suggests, as that reflects the actual implementation in the code.