Styra 0.7.0
This package is deprecated. All new development and functionality have been moved to the Styra.Opa package.
dotnet add package Styra --version 0.7.0
NuGet\Install-Package Styra -Version 0.7.0
<PackageReference Include="Styra" Version="0.7.0" />
paket add Styra --version 0.7.0
#r "nuget: Styra, 0.7.0"
// Install Styra as a Cake Addin
#addin nuget:?package=Styra&version=0.7.0
// Install Styra as a Cake Tool
#tool nuget:?package=Styra&version=0.7.0
OPA C# SDK
SDK Installation
Nuget
dotnet add package Styra.OpenApi
SDK Example Usage (high-level)
All the code examples that follow assume that the high-level SDK module has been imported, and that an OpaClient
instance was created:
using Styra;
public class MyExample {
private string serverURL = "http://opa-host:8181";
private string path = "authz/allow";
private OpaClient opa;
public MyExample() {
opa = new OPAClient(serverURL);
}
// ...
}
Simple query
For a simple boolean response with a dictionary input, use the SDK as follows:
var input = new Dictionary<string, object>() {
{ "user", "alice" },
{ "action", "read" },
};
// (local variable) bool? allowed
var allowed = opa.check(input, path);
// Logic for the `undefined` case ...
if (allowed == null) {
// ...
}
// Normal true/false cases...
if (allowed) {
// ...
}
In the above example, a null
value for allowed
indicates the /authz/allow
endpoint returned undefined
.
A non-null result can be used as a normal true/false value.
<details><summary>HTTP Request</summary>
POST /v1/data/authz/allow
Content-Type: application/json
{ "input": { "user": "alice", "action": "read" } }
</details>
Input types
The check
and query
methods are overloaded for most standard JSON types, which include the following variants for the input
parameter:
C# type | JSON equivalent type |
---|---|
bool |
Boolean |
double |
Number |
string |
String |
List<object> |
Array |
Dictionary<string, object> |
Object |
Result Types
OpaClient.check
For the check
method, the output type is always bool?
, allowing API users to disambiguate undefined
from normal true
/false
results.
OpaClient.query<T>
For the query
method, the output type is configurable using generics, as shown in the example below.
string path = "authz/accounts/max_limit";
double maxLimit = opa.query<double>("example", path);
[!NOTE] For low-level SDK usage, see the sections below.
SDK Example Usage
Example
using Styra.OpenApi;
using Styra.OpenApi.Models.Requests;
using Styra.OpenApi.Models.Components;
var sdk = new OpaApiClient();
ExecutePolicyWithInputRequest req = new ExecutePolicyWithInputRequest() {
Path = "app/rbac",
RequestBody = new ExecutePolicyWithInputRequestBody() {
Input = Input.CreateInputBoolean(
false,
),
},
};
var res = await sdk.ExecutePolicyWithInputAsync(req);
// handle response
Available Resources and Operations
OpaApiClient SDK
- ExecutePolicy - Execute a policy
- ExecutePolicyWithInput - Execute a policy given an input
- Health - Verify the server is operational
Server Selection
Select Server by Index
You can override the default server globally by passing a server index to the serverIndex: number
optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:
# | Server | Variables |
---|---|---|
0 | http://localhost:8181 |
None |
Override Server URL Per-Client
The default server can also be overridden globally by passing a URL to the serverUrl: str
optional parameter when initializing the SDK client instance. For example:
Error Handling
Handling errors in this SDK should largely match your expectations. All operations return a response object or thow an exception. If Error objects are specified in your OpenAPI Spec, the SDK will raise the appropriate type.
Error Object | Status Code | Content Type |
---|---|---|
Styra.OpenApi.Models.Errors.ClientError | 400 | application/json |
Styra.OpenApi.Models.Errors.ServerError | 500 | application/json |
Styra.OpenApi.Models.Errors.SDKException | 4xx-5xx | / |
Example
using Styra.OpenApi;
using System;
using Styra.OpenApi.Models.Errors;
using Styra.OpenApi.Models.Requests;
using Styra.OpenApi.Models.Components;
var sdk = new OpaApiClient();
ExecutePolicyRequest req = new ExecutePolicyRequest() {
Path = "app/rbac",
};
try
{
var res = await sdk.ExecutePolicyAsync(req);
// handle response
}
catch (Exception ex)
{
if (ex is ClientError)
{
// handle exception
}
else if (ex is ServerError)
{
// handle exception
}
else if (ex is Styra.OpenApi.Models.Errors.SDKException)
{
// handle exception
}
}
Development
Maturity
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- newtonsoft.json (>= 13.0.3)
- nodatime (>= 3.1.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|