sdk

package
v0.24.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2024 License: MIT Imports: 9 Imported by: 7

README

dp-permissions-api SDK

Overview

This API contains a client with convenience functions for interacting with the permissions api from other applications. It also contains reusable structs representing models used as payloads in API endpoints.

Example use of the API SDK

package main

import (
	"context"
	"github.com/ONSdigital/dp-permissions-api/sdk"
)

func main() {
	apiClient := sdk.NewClient("http://localhost:25400")

	permissionsBundle, err := c.cache.GetPermissionsBundle(context.Backgroud)
}

Alternative Client instantiation

In the unlikely event that there is a need to use non-default initialisation, it is possible to obtain a new client with customised options and/or underlying http client.

With Options

NB. There are currently no defined options available, this is included for future expansion.

apiClient := sdk.NewClientWithOptions("http://localhost:25400", sdk.Options{})
With Options and a custom http client
import dphttp "github.com/ONSdigital/dp-net/v2/http"

apiClient := sdk.NewClientWithClienter("http://localhost:25400", dphttp.NewClient(), sdk.Options{})

Documentation

Index

Constants

View Source
const (
	Authorization string = "Authorization"
)

package level constants

Variables

View Source
var (

	// ErrGetPermissionsResponseBodyNil error used when a nil response is returned from the permissions API.
	ErrGetPermissionsResponseBodyNil = errors.New("error creating get permissions request http.Request required but was nil")

	// ErrFailedToParsePermissionsResponse error used when an unexpected response body is returned from the permissions API and it fails to parse.
	ErrFailedToParsePermissionsResponse = errors.New("error parsing permissions bundle response body")

	// ErrNotCached error error used when permissions are not found in the cache.
	ErrNotCached = errors.New("permissions bundle not found in the cache")
)

Functions

This section is empty.

Types

type APIClient

type APIClient struct {
	// contains filtered or unexported fields
}

APIClient implementation of permissions.Store that gets permission data from the permissions API

func NewClient

func NewClient(host string) *APIClient

NewClient constructs a new APIClient instance with a default http client and Options.

func NewClientWithClienter

func NewClientWithClienter(host string, httpClient HTTPClient, opts Options) *APIClient

NewClientWithClienter constructs a new APIClient instance.

func NewClientWithOptions

func NewClientWithOptions(host string, opts Options) *APIClient

NewClientWithOptions returns a new APIClient with default http

func (*APIClient) DeletePolicy added in v0.20.0

func (c *APIClient) DeletePolicy(ctx context.Context, id string) error

func (*APIClient) GetPermissionsBundle

func (c *APIClient) GetPermissionsBundle(ctx context.Context) (Bundle, error)

GetPermissionsBundle gets the permissions bundle data from the permissions API.

func (*APIClient) GetPolicy added in v0.20.0

func (c *APIClient) GetPolicy(ctx context.Context, id string) (*models.Policy, error)

func (*APIClient) GetRole added in v0.20.0

func (c *APIClient) GetRole(ctx context.Context, id string) (*models.Roles, error)

func (*APIClient) GetRoles added in v0.20.0

func (c *APIClient) GetRoles(ctx context.Context) (*models.Roles, error)

func (*APIClient) PostPolicy added in v0.20.0

func (c *APIClient) PostPolicy(ctx context.Context, policy models.PolicyInfo) (*models.Policy, error)

func (*APIClient) PutPolicy added in v0.20.0

func (c *APIClient) PutPolicy(ctx context.Context, id string, policy models.Policy) error

type Bundle

type Bundle map[string]EntityIDToPolicies

Bundle is the optimised lookup table for permissions.

type Condition

type Condition struct {
	Attribute string   `json:"attribute"`
	Operator  Operator `json:"operator"`
	Values    []string `json:"values"`
}

Condition is used within a policy to match additional attributes.

type EntityData

type EntityData struct {
	UserID string
	Groups []string
}

EntityData groups the different entity types into a single parameter

type EntityIDToPolicies

type EntityIDToPolicies map[string][]Policy

EntityIDToPolicies maps an entity ID to a slice of policies.

type HTTPClient

type HTTPClient interface {
	Do(ctx context.Context, req *http.Request) (*http.Response, error)
}

HTTPClient is the interface that defines a client for making HTTP requests

type Operator

type Operator string

Operator is used to define a set of supported Condition operators

const (
	OperatorStringEquals Operator = "StringEquals"
	OperatorStartsWith   Operator = "StartsWith"
)

type Options

type Options struct {
	Headers http.Header
}

Options is a struct containing for customised options for the API client

type Policy

type Policy struct {
	ID        string    `json:"id"`
	Condition Condition `json:"condition"`
}

Policy is the policy model as stored in the permissions API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL