sdk

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2023 License: MIT Imports: 9 Imported by: 0

README

dp-feedback-api SDK

Overview

This SDK contains a Go client for interacting with the Feedback API. The client contains a method for each API endpoint so that any Go application wanting to interact with the feedback api can do so. Please refer to the swagger specification as the source of truth of how each endpoint works.

Example use of the API SDK

Initialise new Feedback API client

package main

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

func main() {
    ...
	feedbackAPIClient := sdk.New("http://localhost:28600")
    ...
}
Post feedback

Use the PostFeedback method to send a request to send a feedback email via the feedback API. This is a private endpoint and requires authorisation header.

...
    // Create the feedback model you want to send
    f := &models.Feedback{
		IsPageUseful:      &isPageUsefulVal,
		IsGeneralFeedback: &isGeneralFeedbackVal,
        ...
	}

    // Pass the authorisation token (without the 'Bearer ' prefix) as an SDK Option parameter
    opts := sdk.Options{AuthToken: authToken}

    // Call PostFeedback to send the POST request to the feedback API
    err := apiClient.PostFeedback(ctx, f, opts)
    if err != nil {
        // handle error
    }
...
Handling errors

The error returned from the method contains status code that can be accessed via Status() method and similar to extracting the error message using Error() method; see snippet below:

...
    err := apiClient.PostFeedback(ctx, f, opts)
    if err != nil {
        // Retrieve status code from error
        statusCode := err.Status()
        // Retrieve error message from error
        errorMessage := err.Error()

        // log message, below uses "github.com/ONSdigital/log.go/v2/log" package
        log.Error(ctx, "failed to provide feedback", err, log.Data{"code": statusCode})

        return err
    }
...
Healthcheck

This client extends the default Healthcheck Client. Please view this README for more information.

If your app is using the dp-healthcheck library, then you may register the checker provided by this sdk, like so:

...
    // assuming hc is an instance of HealthCheck, then you may register the feedback API checker
    err := hc.AddCheck("Feedback API", apiClient.Checker)
    if err != nil {
        // handle error
	}
...

Documentation

Index

Constants

View Source
const (
	Service          = "dp-feedback-api"
	FeedbackEndpoint = "%s/v1/feedback"
	Authorization    = "Authorization"
	BearerPrefix     = "Bearer "
)

package level constants

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

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

func New

func New(feedbackAPIURL string) *Client

New constructs a new Client instance with a given feedback api url

func NewWithHealthClient

func NewWithHealthClient(hcCli *healthcheck.Client) *Client

NewWithHealthClient creates a new instance of search API Client, reusing the URL and Clienter from the provided healthcheck client

func (*Client) Checker

func (cli *Client) Checker(ctx context.Context, check *health.CheckState) error

Checker calls search api health endpoint and returns a check object to the caller

func (*Client) Health

func (cli *Client) Health() *healthcheck.Client

Health returns the underlying Healthcheck Client for this search API client

func (*Client) PostFeedback

func (cli *Client) PostFeedback(ctx context.Context, feedback *models.Feedback, options Options) *sdkError.StatusError

PostFeedback sends the provided feedback model to the feedback API via a post call

func (*Client) URL

func (cli *Client) URL() string

URL returns the URL used by this client

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 Options

type Options struct {
	AuthToken string
}

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

func (*Options) SetAuth

func (o *Options) SetAuth(req *http.Request)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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