mopinion

package module
v0.0.0-...-e558d6f Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2022 License: BSD-3-Clause Imports: 15 Imported by: 0

README

mopinion

Build Status codecov

Go client library for mopinion. The aim is to make the communication easier with the Mopinion API. Go doc can be found at https://godoc.org/github.com/oylmz/mopinion

Usage

package main

import (
	"context"
	"log"
	"os"

	"github.com/oylmz/mopinion"
)

func main() {
	basicCredentialProvider := mopinion.NewBasicCredentialProvider(os.Getenv("MOPINION_PUBLIC_KEY"),
		os.Getenv("MOPINION_PRIVATE_KEY"))
	client, _ := mopinion.NewClient(nil, basicCredentialProvider)

	ctx := context.TODO()
	var err error
	if _, _, err = client.Token.Get(ctx); err != nil {
		log.Fatalf("get the token: %s", err)
	}

	account, _, err := client.Account.Get(ctx)
	if err != nil {
		log.Fatalf("get account: %s", err)
	}

	log.Println("account: %+v", account)
}

Contributing

Please feel free to contribute if any updates or changes happen in the Mopinion API.

License

This library is distributed under the BSD-style license found in the LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckResponse

func CheckResponse(r *http.Response) error

CheckResponse checks the response if the response is either an error or not.

Types

type Account

type Account struct {
	Meta          Meta `json:"_meta,omitempty"`
	Name          string
	Package       string
	EndDate       string
	NumberUsers   int `json:"number_users"`
	NumberCharts  int `json:"number_charts"`
	NumberForms   int `json:"number_forms"`
	NumberReports int `json:"number_reports"`
	Reports       []Report
}

Account is a struct which reflects to mopinion Account resource.

type AccountInterface

type AccountInterface interface {
	Get(ctx context.Context) (*Account, *Response, error)
}

AccountInterface holds only one method for retrieving the account.

type AccountService

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

AccountService implements AccountInterface.

func (*AccountService) Get

func (s *AccountService) Get(ctx context.Context) (*Account, *Response, error)

Get returns the account corresponded to given public key.

type AnswerOptions

type AnswerOptions struct {
	Scale       int
	StartAtZero bool `json:"start_at_zero"`
	Type        string
}

AnswerOptions is a struct which reflects to mopinion AnswerOptions resource.

type AuthenticationError

type AuthenticationError ErrorResponse

AuthenticationError occurs when an invalid token is provided.

func (*AuthenticationError) Error

func (r *AuthenticationError) Error() string

type BasicCredentialProvider

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

BasicCredentialProvider implements CredentialProvider.

func (*BasicCredentialProvider) Keys

Keys returns public and private keys respectively.

type Client

type Client struct {

	// BaseURL holds the url for the mopinion api.
	BaseURL *url.URL

	// UserAgent holds the agent name while communicating with mopinion api.
	UserAgent string

	// Services used for talking to different parts of the Mopinion API.
	Token       TokenInterface
	Account     AccountInterface
	Deployments DeploymentsInterface
	Datasets    DatasetsInterface
	Fields      FieldsInterface
	Feedback    FeedbackInterface
	Reports     ReportsInterface
	// contains filtered or unexported fields
}

Client represents the Mopinion client.

func NewClient

func NewClient(httpClient *http.Client, credentialProvider CredentialProvider) (*Client, error)

NewClient returns a new Mopinion API client.

func (*Client) AddAutheticationToken

func (c *Client) AddAutheticationToken(req *http.Request) error

AddAutheticationToken adds an x-auth-token.

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Response, error)

Do makes a request to mopinion API and returns the response.

func (*Client) IsAuthenticationRequired

func (c *Client) IsAuthenticationRequired(method, urlStr string) bool

IsAuthenticationRequired checks if an auth token needs to be passed along with the given relative URL.

func (*Client) NewRequest

func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error)

NewRequest creates an API request.

func (*Client) SetToken

func (c *Client) SetToken(token *Token)

SetToken sets a token.

type CredentialProvider

type CredentialProvider interface {
	// Keys returns public and private keys respectively.
	Keys() (string, string, error)
}

CredentialProvider is an interface to be used to retrieve keys to access the Mopinion API.

func NewBasicCredentialProvider

func NewBasicCredentialProvider(publicKey, privateKey string) CredentialProvider

NewBasicCredentialProvider returns a BasicCredentialProvider.

type Dataset

type Dataset struct {
	Meta        *Meta  `json:"_meta,omitempty"`
	ID          int    `json:"id,omitempty"`
	Name        string `json:"name,omitempty"`
	ReportID    int    `json:"report_id,omitempty"`
	Description string `json:"description,omitempty"`
	DataSource  string `json:"data_source,omitempty"`
}

Dataset is a struct which reflects to mopinion Dataset resource.

type DatasetsInterface

type DatasetsInterface interface {
	Get(ctx context.Context, datasetID int) (*Dataset, *Response, error)
	Add(ctx context.Context, dataset *Dataset) (*Dataset, *Response, error)
	Update(ctx context.Context, dataset *Dataset) (*Dataset, *Response, error)
	Delete(ctx context.Context, datasetID int, dryRun bool) (*DeleteResponse, *Response, error)
}

DatasetsInterface contains CRUD methods around Dataset model.

type DatasetsService

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

DatasetsService implemets DatasetsInterface, holds a reference of service back to mopinion client.

func (*DatasetsService) Add

func (s *DatasetsService) Add(ctx context.Context, dataset *Dataset) (*Dataset, *Response, error)

Add creates a new dataset if validation passes. Dataset report id or name cannot be empty.

func (*DatasetsService) Delete

func (s *DatasetsService) Delete(ctx context.Context, datasetID int, dryRun bool) (*DeleteResponse, *Response, error)

Delete removes the record with given id. If dry-run is true, It rehearses the operation.

func (*DatasetsService) Get

func (s *DatasetsService) Get(ctx context.Context, datasetID int) (*Dataset, *Response, error)

Get returns Dataset by given id

func (*DatasetsService) Update

func (s *DatasetsService) Update(ctx context.Context, dataset *Dataset) (*Dataset, *Response, error)

Update simply edits the given dataset.

type DeleteResponse

type DeleteResponse struct {
	Executed          bool
	ResourcesAffected map[string]interface{} `json:"resources_affected"`
}

DeleteResponse is a struct which reflects to mopinion DeleteResponse resource.

type Deployment

type Deployment struct {
	Key  string
	Name string
}

Deployment is a struct which reflects to mopinion Deployment resource.

type Deployments

type Deployments struct {
	Meta        Meta `json:"_meta"`
	Deployments []Deployment
}

Deployments is a struct which reflects to mopinion Deployments resource.

func (*Deployments) UnmarshalJSON

func (d *Deployments) UnmarshalJSON(buf []byte) (err error)

UnmarshalJSON implements Unmarshaler interface.

type DeploymentsInterface

type DeploymentsInterface interface {
	Get(ctx context.Context) (*Deployments, *Response, error)
	Add(ctx context.Context, deployment *Deployment) (*Deployments, *Response, error)
	Delete(ctx context.Context, deploymentKey string, dryRun bool) (*DeleteResponse, *Response, error)
}

DeploymentsInterface contains CRUD methods for Deployments model.

type DeploymentsService

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

DeploymentsService implements DeploymentsInterface.

func (*DeploymentsService) Add

func (s *DeploymentsService) Add(ctx context.Context, deployment *Deployment) (*Deployments, *Response, error)

Add creates a new Deployment, if the given deployment is valid.

func (*DeploymentsService) Delete

func (s *DeploymentsService) Delete(ctx context.Context, deploymentKey string, dryRun bool) (*DeleteResponse, *Response, error)

Delete removes the deployment for given given ID.

func (*DeploymentsService) Get

Get returns Deployments

type ErrorCode

type ErrorCode int

ErrorCode represented

type ErrorResponse

type ErrorResponse struct {
	Response  *http.Response
	Status    int    `json:"status"`
	ErrorCode int    `json:"error_code"`
	Title     string `json:"title"`
	Type      string `json:"type"`
}

ErrorResponse represent the returning error struct. Implements the error interface.

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

Error returns an explainatory message.

type Feedback

type Feedback struct {
	Meta Meta `json:"_meta"`
	Data []FeedbackData
}

Feedback is a struct which reflects to mopinion Feedback resource.

type FeedbackData

type FeedbackData struct {
	Created   string
	DatasetID int `json:"dataset_id"`
	ID        int
	ReportID  int `json:"report_id"`
	Tags      []string
	Fields    []FeedbackField
}

FeedbackData is a struct which reflects to mopinion FeedbackData resource.

type FeedbackField

type FeedbackField struct {
	Key   string
	Label string
	Value interface{}
}

FeedbackField is a struct which reflects to mopinion FeedbackField resource.

type FeedbackInterface

type FeedbackInterface interface {
	GetByDataset(ctx context.Context, datasetID int, options *PaginationOptions, filters *FilterCollection) (*Feedback, *Response, error)
	GetByReport(ctx context.Context, reportID int, options *PaginationOptions, filters *FilterCollection) (*Feedback, *Response, error)
}

FeedbackInterface holds two methods that return feedback by a given dataset or report. FeedbackInterface accepts pagination options and filters.

type FeedbackService

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

FeedbackService implements FeedbackInterface.

func (*FeedbackService) GetByDataset

func (s *FeedbackService) GetByDataset(ctx context.Context, datasetID int, options *PaginationOptions, filters *FilterCollection) (*Feedback, *Response, error)

GetByDataset returns feedback by given dataset id, pagination options and filters.

func (*FeedbackService) GetByReport

func (s *FeedbackService) GetByReport(ctx context.Context, reportID int, options *PaginationOptions, filters *FilterCollection) (*Feedback, *Response, error)

GetByReport returns feedback by given report id, pagination options and filters.

type FieldData

type FieldData struct {
	AnswerOptions *AnswerOptions `json:"answer_options"`
	AnswerValues  []string       `json:"answer_values"`
	DatasetID     int
	Key           string
	Label         string
	ReportID      int    `json:"report_id"`
	ShortLabel    string `json:"short_label"`
	Type          string
}

FieldData is a struct which reflects to mopinion FieldData resource.

type Fields

type Fields struct {
	Meta Meta `json:"_meta,omitempty"`
	Data []FieldData
}

Fields is a struct which reflects to mopinion Fields resource.

type FieldsInterface

type FieldsInterface interface {
	GetByDataset(ctx context.Context, datasetID int) (*Fields, *Response, error)
	GetByReport(ctx context.Context, reportID int) (*Fields, *Response, error)
}

FieldsInterface has two methods returning fields for a specific dataset or report.

type FieldsService

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

FieldsService implements FieldsInterface

func (*FieldsService) GetByDataset

func (s *FieldsService) GetByDataset(ctx context.Context, datasetID int) (*Fields, *Response, error)

GetByDataset returns the fields of given dataset id

func (*FieldsService) GetByReport

func (s *FieldsService) GetByReport(ctx context.Context, reportID int) (*Fields, *Response, error)

GetByReport returns the fields of given report id

type Filter

type Filter struct {
	Key      FilterKey
	Modifier FilterModifier
	Value    string
}

Filter is a struct composed of key, modifier and its value. For more information, look at the link below. https://developer.mopinion.com/api/#section/Requests-and-Responses/Filters

func (Filter) Build

func (f Filter) Build() string

Build returns the string to be appended to the URL.

type FilterCollection

type FilterCollection struct {
	Filters []Filter
}

FilterCollection holds the filters to be applied on feedback data.

type FilterKey

type FilterKey string

FilterKey type represents the possible filters

const (
	// Date refers to the time when a record is created.
	Date FilterKey = "date"
	// Rating is a geenral numeric rating, its value should be numeric.
	Rating FilterKey = "rating"
	// Nps is Net Promotor Score, its value should be between 0 and 10.
	Nps FilterKey = "nps"
	// Ces is Customer Effort Score, its value should be between 1 and 5.
	Ces FilterKey = "ces"
	// CesInverse is Customer Effort Score
	CesInverse FilterKey = "ces_inverse"
	// Gcr is Goal Completion Rate. Option are no, partly, yes.
	Gcr FilterKey = "gcr"
	// Tags can be assigned to the feedback items.
	Tags FilterKey = "tags"
)

type FilterModifier

type FilterModifier string

FilterModifier type represents the possible modifiers

const (
	// Not is used for logical not
	Not FilterModifier = "!"
	// Lt means less than
	Lt FilterModifier = "<"
	// Lte means less than or equal
	Lte FilterModifier = "<<"
	// Gt means greater than
	Gt FilterModifier = ">"
	// Gte means greater than or equal
	Gte FilterModifier = ">>"
)

type Meta

type Meta struct {
	Code     int
	Count    int
	HasMore  bool `json:"has_more"`
	Message  string
	Next     interface{} // boolean false, or a string with a url
	Previous interface{}
	Total    int
}

Meta is a struct that takes place in responses from Mopinion API, with the json tag `_meta`. It holds basic information about the resources.

type PaginationOptions

type PaginationOptions struct {
	Page  int    `url:"page,omitempty"`
	Limit int    `url:"limit,omitempty"`
	Sort  string `url:"sort,omitempty"`
	Order string `url:"order,omitempty"`
}

PaginationOptions holds info for pagination.

type Report

type Report struct {
	Meta        *Meta  `json:"_meta,omitempty"`
	ID          int    `json:"id,omitempty"`
	Name        string `json:"name,omitempty"`
	Description string `json:"description,omitempty"`
	Language    string `json:"language,omitempty"`
	Created     string `json:"created,omitempty"`
	Datasets    []Dataset
}

Report is a struct which reflects to mopinion Report resource.

type ReportsInterface

type ReportsInterface interface {
	Get(ctx context.Context, reportID int) (*Report, *Response, error)
	Add(ctx context.Context, report *Report) (*Report, *Response, error)
	Update(ctx context.Context, report *Report) (*Report, *Response, error)
	Delete(ctx context.Context, reportID int, dryRun bool) (*DeleteResponse, *Response, error)
}

ReportsInterface contains CRUD methods for Report model.

type ReportsService

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

ReportsService implements ReportsInterface.

func (*ReportsService) Add

func (s *ReportsService) Add(ctx context.Context, report *Report) (*Report, *Response, error)

Add creates a new Report, if the given report is valid.

func (*ReportsService) Delete

func (s *ReportsService) Delete(ctx context.Context, reportID int, dryRun bool) (*DeleteResponse, *Response, error)

Delete removes the report for given given ID.

func (*ReportsService) Get

func (s *ReportsService) Get(ctx context.Context, reportID int) (*Report, *Response, error)

Get returns a Report for given report ID.

func (*ReportsService) Update

func (s *ReportsService) Update(ctx context.Context, report *Report) (*Report, *Response, error)

Update edits an existing report model.

type Response

type Response struct {
	*http.Response
}

Response is wrapper around http.Response.

type ServerError

type ServerError ErrorResponse

ServerError is unexpected server side error, coming through Mopinion API.

func (*ServerError) Error

func (r *ServerError) Error() string

type Token

type Token struct {
	Token string
}

Token contains a string which is retrieved by Token service. It is used to generate a hmac signature.

type TokenInterface

type TokenInterface interface {
	Get(ctx context.Context) (*Token, *Response, error)
}

TokenInterface has only one method that returns a token by given public and private keys.

type TokenService

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

TokenService implements TokenInterface.

func (*TokenService) Get

func (s *TokenService) Get(ctx context.Context) (*Token, *Response, error)

Get returns a token by passing the keys with basic authentication.

Jump to

Keyboard shortcuts

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