moesifapi

package module
v1.0.4-0...-8daa23e Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

README

MoesifApi Lib for Golang

Send REST API Events to Moesif for error analysis

Source Code on GitHub

Introduction

This lib has both synchronous and async methods:

  • The synchronous methods call the Moesif API directly
  • The async methods (Which start with QueueXXX) will queue the requests into batches and flush buffers periodically.

Async methods are expected to be the common use case

How to install

Run the following commands:

moesifapi-go can be installed like any other Go library through go get:

go get github.com/moesif/moesifapi-go

Or, if you are already using Go Modules, specify a version number as well:

go get github.com/moesif/moesifapi-go@v1.0.3

How to use

(See examples/api_test.go for usage examples)

Create single event
import "github.com/moesif/moesifapi-go"
import "github.com/moesif/moesifapi-go/models"
import "time"

apiClient := moesifapi.NewAPI("my_application_id")

reqTime := time.Now().UTC()
apiVersion := "1.0"
ipAddress := "61.48.220.123"

req := models.EventRequestModel{
	Time:       &reqTime,
	Uri:        "https://api.acmeinc.com/widgets",
	Verb:       "GET",
	ApiVersion: &apiVersion,
	IpAddress:  &ipAddress,
	Headers: map[string]interface{}{
		"ReqHeader1": "ReqHeaderValue1",
	},
	Body: nil,
}

rspTime := time.Now().UTC().Add(time.Duration(1) * time.Second)

rsp := models.EventResponseModel{
	Time:      &rspTime,
	Status:    500,
	IpAddress: nil,
	Headers: map[string]interface{}{
		"RspHeader1": "RspHeaderValue1",
	},
	Body: map[string]interface{}{
		"Key1": "Value1",
		"Key2": 12,
		"Key3": map[string]interface{}{
			"Key3_1": "SomeValue",
		},
	},
}

sessionToken := "23jdf0owekfmcn4u3qypxg09w4d8ayrcdx8nu2ng]s98y18cx98q3yhwmnhcfx43f"
userId := "my_user_id"
companyId := "my_company_id"
metadata := map[string]interface{}{
		"Key1": "metadata",
		"Key2": 12,
		"Key3": map[string]interface{}{
			"Key3_1": "SomeValue",
		},
	}

event := models.EventModel{
  Request:      req,
  Response:     rsp,
  SessionToken: &sessionToken,
  Tags:         nil,
  UserId:       &userId,
  CompanyId: 	&companyId,
  Metadata: 	&metadata,
}

// Queue the events
err := apiClient.QueueEvent(&event)

// Create the events synchronously
err := apiClient.CreateEvent(&event)

Create batches of events with bulk API
import "github.com/moesif/moesifapi-go"
import "github.com/moesif/moesifapi-go/models"
import "time"

apiClient := moesifapi.NewAPI("my_application_id")

reqTime := time.Now().UTC()
apiVersion := "1.0"
ipAddress := "61.48.220.123"

req := models.EventRequestModel{
	Time:       &reqTime,
	Uri:        "https://api.acmeinc.com/widgets",
	Verb:       "GET",
	ApiVersion: &apiVersion,
	IpAddress:  &ipAddress,
	Headers: map[string]interface{}{
		"ReqHeader1": "ReqHeaderValue1",
	},
	Body: nil,
}

rspTime := time.Now().UTC().Add(time.Duration(1) * time.Second)

rsp := models.EventResponseModel{
	Time:      &rspTime,
	Status:    500,
	IpAddress: nil,
	Headers: map[string]interface{}{
		"RspHeader1": "RspHeaderValue1",
	},
	Body: map[string]interface{}{
		"Key1": "Value1",
		"Key2": 12,
		"Key3": map[string]interface{}{
			"Key3_1": "SomeValue",
		},
	},
}

sessionToken := "23jdf0owekfmcn4u3qypxg09w4d8ayrcdx8nu2ng]s98y18cx98q3yhwmnhcfx43f"
userId := "my_user_id"
companyId := "my_company_id"
metadata := map[string]interface{}{
		"Key1": "metadata",
		"Key2": 12,
		"Key3": map[string]interface{}{
			"Key3_1": "SomeValue",
		},
	}

event := models.EventModel{
  Request:      req,
  Response:     rsp,
  SessionToken: &sessionToken,
  Tags:         nil,
  UserId:       &userId,
  CompanyId: 	&companyId,
  Metadata: 	&metadata,
}

events := make([]*models.EventModel, 20)
for i := 0; i < 20; i++ {
	events[i] = &event
}

// Queue the events
err := apiClient.QueueEvents(events)

// Create the events batch synchronously
err := apiClient.CreateEventsBatch(event)

Update a Single User

Create or update a user profile in Moesif. The metadata field can be any customer demographic or other info you want to store. Only the UserId field is required. For details, visit the Go API Reference.

import "github.com/moesif/moesifapi-go"
import "github.com/moesif/moesifapi-go/models"

func literalFieldValue(value string) *string {
    return &value
}

apiClient := moesifapi.NewAPI("YOUR_COLLECTOR_APPLICATION_ID")

// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#users for campaign schema
campaign := models.CampaignModel {
  UtmSource: literalFieldValue("google"),
  UtmMedium: literalFieldValue("cpc"), 
  UtmCampaign: literalFieldValue("adwords"),
  UtmTerm: literalFieldValue("api+tooling"),
  UtmContent: literalFieldValue("landing"),
}

// metadata can be any custom dictionary
metadata := map[string]interface{}{
  "email": "john@acmeinc.com",
  "first_name": "John",
  "last_name": "Doe",
  "title": "Software Engineer",
  "sales_info": map[string]interface{}{
      "stage": "Customer",
      "lifetime_value": 24000,
      "account_owner": "mary@contoso.com",
  },
}

// Only UserId is required
user := models.UserModel{
  UserId:  "12345",
  CompanyId:  literalFieldValue("67890"), // If set, associate user with a company object
  Campaign:  &campaign,
  Metadata:  &metadata,
}

// Queue the user asynchronously
err := apiClient.QueueUser(&user)

// Update the user synchronously
err := apiClient.UpdateUser(&user)

Update Users in Batch

Similar to UpdateUser, but used to update a list of users in one batch. Only the UserId field is required. For details, visit the Go API Reference.

import "github.com/moesif/moesifapi-go"
import "github.com/moesif/moesifapi-go/models"

func literalFieldValue(value string) *string {
    return &value
}

apiClient := moesifapi.NewAPI("YOUR_COLLECTOR_APPLICATION_ID")

// List of Users
var users []*models.UserModel

// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#users for campaign schema
campaign := models.CampaignModel {
  UtmSource: literalFieldValue("google"),
  UtmMedium: literalFieldValue("cpc"), 
  UtmCampaign: literalFieldValue("adwords"),
  UtmTerm: literalFieldValue("api+tooling"),
  UtmContent: literalFieldValue("landing"),
}

// metadata can be any custom dictionary
metadata := map[string]interface{}{
  "email": "john@acmeinc.com",
  "first_name": "John",
  "last_name": "Doe",
  "title": "Software Engineer",
  "sales_info": map[string]interface{}{
      "stage": "Customer",
      "lifetime_value": 24000,
      "account_owner": "mary@contoso.com",
  },
}

// Only UserId is required
userA := models.UserModel{
  UserId:  "12345",
  CompanyId:  literalFieldValue("67890"), // If set, associate user with a company object
  Campaign:  &campaign,
  Metadata:  &metadata,
}

users = append(users, &userA)

// Queue the user asynchronously
err := apiClient.QueueUsers(&users)

// Update the user synchronously
err := apiClient.UpdateUsersBatch(&users)

Update a Single Company

Create or update a company profile in Moesif. The metadata field can be any company demographic or other info you want to store. Only the CompanyId field is required. For details, visit the Go API Reference.

import "github.com/moesif/moesifapi-go"
import "github.com/moesif/moesifapi-go/models"

func literalFieldValue(value string) *string {
    return &value
}

apiClient := moesifapi.NewAPI("YOUR_COLLECTOR_APPLICATION_ID")

// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#update-a-company for campaign schema
campaign := models.CampaignModel {
  UtmSource: literalFieldValue("google"),
  UtmMedium: literalFieldValue("cpc"), 
  UtmCampaign: literalFieldValue("adwords"),
  UtmTerm: literalFieldValue("api+tooling"),
  UtmContent: literalFieldValue("landing"),
}

// metadata can be any custom dictionary
metadata := map[string]interface{}{
  "org_name": "Acme, Inc",
  "plan_name": "Free",
  "deal_stage": "Lead",
  "mrr": 24000,
  "demographics": map[string]interface{}{
      "alexa_ranking": 500000,
      "employee_count": 47,
  },
}

// Prepare company model
company := models.CompanyModel{
    CompanyId:        "67890",  // The only required field is your company id
    CompanyDomain:    literalFieldValue("acmeinc.com"), // If domain is set, Moesif will enrich your profiles with publicly available info 
    Campaign:         &campaign,
    Metadata:         &metadata,
}

// Queue the company asynchronously
apiClient.QueueCompany(&company)

// Update the company synchronously
err := apiClient.UpdateCompany(&company)

Update Companies in Batch

Similar to updateCompany, but used to update a list of companies in one batch. Only the CompanyId field is required. For details, visit the Go API Reference.

import "github.com/moesif/moesifapi-go"
import "github.com/moesif/moesifapi-go/models"

func literalFieldValue(value string) *string {
    return &value
}

apiClient := moesifapi.NewAPI("YOUR_COLLECTOR_APPLICATION_ID")

// List of Companies
var companies []*models.CompanyModel

// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#update-a-company for campaign schema
campaign := models.CampaignModel {
  UtmSource: literalFieldValue("google"),
  UtmMedium: literalFieldValue("cpc"), 
  UtmCampaign: literalFieldValue("adwords"),
  UtmTerm: literalFieldValue("api+tooling"),
  UtmContent: literalFieldValue("landing"),
}

// metadata can be any custom dictionary
metadata := map[string]interface{}{
  "org_name": "Acme, Inc",
  "plan_name": "Free",
  "deal_stage": "Lead",
  "mrr": 24000,
  "demographics": map[string]interface{}{
      "alexa_ranking": 500000,
      "employee_count": 47,
  },
}

// Prepare company model
companyA := models.CompanyModel{
    CompanyId:        "67890",  // The only required field is your company id
    CompanyDomain:    literalFieldValue("acmeinc.com"), // If domain is set, Moesif will enrich your profiles with publicly available info 
    Campaign:         &campaign,
    Metadata:         &metadata,
}

companies = append(companies, &companyA)

// Queue the company asynchronously
apiClient.QueueCompanies(&companies)

// Update the company synchronously
err := apiClient.UpdateCompaniesBatch(&companies)
Health Check
go get github.com/moesif/moesifapi-go/health;

How To Test:

git clone https://github.com/moesif/moesifapi-go
cd moesifapi-go/examples
# Be sure to edit the examples/api_test.go to change the application id to your real one obtained from Moesif.
# var applicationId = "Your Application Id"
go test  -v

Other integrations

To view more documentation on integration options, please visit the Integration Options Documentation.

Documentation

Overview

* moesifapi-go

* moesifapi-go

* moesifapi-go

Index

Constants

View Source
const BaseURI string = "https://api.drhttp.com/moesif"

* The base Uri for API calls

View Source
const Version string = "1.0.3"

* Version of this lib

Variables

View Source
var Config = config{QueueSize: 256}

Functions

This section is empty.

Types

type API

type API interface {

	/**
	 * Queue Single API Event Call to be created
	 * @param    *models.EventModel        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	QueueEvent(*models.EventModel) error

	/**
	 * Queue multiple API Events to be added
	 * @param    []*models.EventModel        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	QueueEvents([]*models.EventModel) error

	/**
	 * Queue Single User to be updated
	 * @param    *models.UserModel        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	QueueUser(*models.UserModel) error

	/**
	 * Queue multiple Users to be updated
	 * @param    []*models.UserModel        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	QueueUsers([]*models.UserModel) error

	/**
	 * Queue Single Company to be added
	 * @param    *models.CompanyModel        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	QueueCompany(*models.CompanyModel) error

	/**
	 * Queue multiple companies to be added
	 * @param    []*models.CompanyModel        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	QueueCompanies([]*models.CompanyModel) error

	/**
	 * Add Single API Event Call
	 * @param    *models.EventModel        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	CreateEvent(*models.EventModel) (http.Header, error)

	/**
	 * Add multiple API Events in a single batch (batch size must be less than 250kb)
	 * @param    []*models.EventModel        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	CreateEventsBatch([]*models.EventModel) (http.Header, error)

	/**
	 * Update a Single User
	 * @param    *models.UserModel        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	UpdateUser(*models.UserModel) error

	/**
	 * Update multiple Users in a single batch (batch size must be less than 250kb)
	 * @param    []*models.UserModel        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	UpdateUsersBatch([]*models.UserModel) error

	/**
	 * Get Application configuration
	 * @param    nil        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	GetAppConfig() (*http.Response, error)

	/**
	 * Update a Single Company
	 * @param    *models.CompanyModel        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	UpdateCompany(*models.CompanyModel) error

	/**
	 * Update multiple companies in a single batch (batch size must be less than 250kb)
	 * @param    []*models.CompanyModel        body     parameter: Required
	 * @return	Returns the  response from the API call
	 */
	UpdateCompaniesBatch([]*models.CompanyModel) error

	Flush()

	Close()
}

* Interface for the Client

func NewAPI

func NewAPI(moesifApplicationId string) API

* Factory for the API interaface returning Client

type Client

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

* Client structure as interface implementation

func NewClient

func NewClient() *Client

func (*Client) Close

func (c *Client) Close()

func (*Client) CreateEvent

func (c *Client) CreateEvent(event *models.EventModel) (http.Header, error)

*

  • Add Single API Event Call
  • @param *models.EventModel body parameter: Required
  • @return Returns the response from the API call

func (*Client) CreateEventsBatch

func (c *Client) CreateEventsBatch(events []*models.EventModel) (http.Header, error)

*

  • Add multiple API Events in a single batch (batch size must be less than 250kb)
  • @param []*models.EventModel body parameter: Required
  • @return Returns the response from the API call

func (*Client) Flush

func (c *Client) Flush()

func (*Client) GetAppConfig

func (c *Client) GetAppConfig() (*http.Response, error)

*

  • Get Application configuration
  • @param nil parameter: Required
  • @return Returns the response from the API call

func (*Client) QueueCompanies

func (c *Client) QueueCompanies(u []*models.CompanyModel) error

*

  • Queue multiple companies to be added
  • @param []*models.UserModel body parameter: Required
  • @return Returns the response from the API call

func (*Client) QueueCompany

func (c *Client) QueueCompany(u *models.CompanyModel) error

*

  • Queue Single Company to be added
  • @param *models.CompanyModel body parameter: Required
  • @return Returns the response from the API call

func (*Client) QueueEvent

func (c *Client) QueueEvent(e *models.EventModel) error

*

  • Queue Single API Event Call to be created
  • @param *models.EventModel body parameter: Required
  • @return Returns the response from the API call

func (*Client) QueueEvents

func (c *Client) QueueEvents(e []*models.EventModel) error

*

  • Queue multiple API Events to be added
  • @param []*models.EventModel body parameter: Required
  • @return Returns the response from the API call

func (*Client) QueueUser

func (c *Client) QueueUser(u *models.UserModel) error

*

  • Queue Single User to be updated
  • @param *models.UserModel body parameter: Required
  • @return Returns the response from the API call

func (*Client) QueueUsers

func (c *Client) QueueUsers(u []*models.UserModel) error

*

  • Queue multiple Users to be updated
  • @param []*models.UserModel body parameter: Required
  • @return Returns the response from the API call

func (*Client) UpdateCompaniesBatch

func (c *Client) UpdateCompaniesBatch(companies []*models.CompanyModel) error

*

  • Update multiple companies in a single batch (batch size must be less than 250kb)
  • @param []*models.CompanyModel body parameter: Required
  • @return Returns the response from the API call

func (*Client) UpdateCompany

func (c *Client) UpdateCompany(company *models.CompanyModel) error

*

  • Update a Single Company
  • @param *models.CompanyModel body parameter: Required
  • @return Returns the response from the API call

func (*Client) UpdateUser

func (c *Client) UpdateUser(user *models.UserModel) error

*

  • Update a Single User
  • @param *models.UserModel body parameter: Required
  • @return Returns the response from the API call

func (*Client) UpdateUsersBatch

func (c *Client) UpdateUsersBatch(users []*models.UserModel) error

*

  • Update multiple Users in a single batch (batch size must be less than 250kb)
  • @param []*models.UserModel body parameter: Required
  • @return Returns the response from the API call

Directories

Path Synopsis
* moesifapi-go * moesifapi-go
* moesifapi-go * moesifapi-go
* moesifapi-go
* moesifapi-go

Jump to

Keyboard shortcuts

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