meraki

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2024 License: MPL-2.0 Imports: 15 Imported by: 0

README

Tests

go-meraki

go-meraki is a Go client library for Cisco Meraki. It is based on Nathan's excellent goaci module and features a simple, extensible API and advanced JSON manipulation.

Getting Started

Installing

To start using go-meraki, install Go and go get:

$ go get -u github.com/netascode/go-meraki

Basic Usage
package main

import "github.com/netascode/go-meraki"

func main() {
    client, _ := meraki.NewClient("abc123")

    res, _ := client.Get("/organizations")
    println(res.Get("0.name").String())
}

This will print something like:

My First Organization
Result manipulation

meraki.Result uses GJSON to simplify handling JSON results. See the GJSON documentation for more detail.

res, _ := client.Get("/organizations")

for _, obj := range res.Array() {
    println(obj.Get("@pretty").String()) // pretty print network objects
}
POST data creation

meraki.Body is a wrapper for SJSON. SJSON supports a path syntax simplifying JSON creation.

body := meraki.Body{}.
    Set("name", "NewNetwork1").
    Set("productTypes", []string{"switch"})
client.Post("/organizations/123456/networks", body.Str)

Documentation

See the documentation for more details.

Documentation

Overview

Package meraki is a Cisco Meraki REST client library for Go.

Index

Constants

View Source
const DefaultBackoffDelayFactor float64 = 3
View Source
const DefaultBackoffMaxDelay int = 60
View Source
const DefaultBackoffMinDelay int = 2
View Source
const DefaultMaxRetries int = 3

Variables

This section is empty.

Functions

func BackoffDelayFactor

func BackoffDelayFactor(x float64) func(*Client)

BackoffDelayFactor modifies the backoff delay factor from the default of 3.

func BackoffMaxDelay

func BackoffMaxDelay(x int) func(*Client)

BackoffMaxDelay modifies the maximum delay between two retries from the default of 60.

func BackoffMinDelay

func BackoffMinDelay(x int) func(*Client)

BackoffMinDelay modifies the minimum delay between two retries from the default of 2.

func BaseUrl

func BaseUrl(x string) func(*Client)

BaseUrl modifies the API base URL. Default value is 'https://api.meraki.com/api/v1'.

func MaxRetries

func MaxRetries(x int) func(*Client)

MaxRetries modifies the maximum number of retries from the default of 3.

func NoLogPayload

func NoLogPayload(req *Req)

NoLogPayload prevents logging of payloads.

func RequestPerSecond

func RequestPerSecond(x int) func(*Client)

RequestPerSecond modifies the maximum number of requests per second. Default value is 10.

func RequestTimeout

func RequestTimeout(x time.Duration) func(*Client)

RequestTimeout modifies the HTTP request timeout from the default of 60 seconds.

func UserAgent

func UserAgent(x string) func(*Client)

UserAgent modifies the HTTP user agent string. Default value is 'go-meraki netascode'.

Types

type Body

type Body struct {
	Str string
}

Body wraps SJSON for building JSON body strings. Usage example:

Body{}.Set("name", "ABC").Str

func (Body) Delete

func (body Body) Delete(path string) Body

Delete deletes a JSON path.

func (Body) Res

func (body Body) Res() Res

Res creates a Res object, i.e. a GJSON result object.

func (Body) Set

func (body Body) Set(path string, value interface{}) Body

Set sets a JSON path to a value.

func (Body) SetRaw

func (body Body) SetRaw(path, rawValue string) Body

SetRaw sets a JSON path to a raw string value. This is primarily used for building up nested structures, e.g.:

Body{}.SetRaw("children", Body{}.Set("name", "New").Str).Str

type Client

type Client struct {
	// HttpClient is the *http.Client used for API requests
	HttpClient *http.Client
	// BaseUrl is the Meraki Dashboard Base API Url, default is https://api.meraki.com/api/v1
	BaseUrl string
	// ApiToken is the current API token
	ApiToken string
	// UserAgent is the HTTP User-Agent string
	UserAgent string
	// Maximum number of requests per second
	RequestPerSecond int
	// Maximum number of retries
	MaxRetries int
	// Minimum delay between two retries
	BackoffMinDelay int
	// Maximum delay between two retries
	BackoffMaxDelay int
	// Backoff delay factor
	BackoffDelayFactor float64
	// Rate limiter bucket
	RateLimiterBucket *ratelimit.Bucket
	// contains filtered or unexported fields
}

Client is an HTTP Meraki client. Use meraki.NewClient to initiate a client. This will ensure proper cookie handling and processing of modifiers.

Requests are protected from concurrent writing (concurrent DELETE/POST/PUT), across all API paths. Any GET requests, or requests from different clients are not protected against concurrent writing.

func NewClient

func NewClient(token string, mods ...func(*Client)) (Client, error)

NewClient creates a new Meraki HTTP client. Pass modifiers in to modify the behavior of the client, e.g.

client, _ := NewClient("abc123", RequestTimeout(120))

func (*Client) Backoff

func (client *Client) Backoff(attempts int) bool

Backoff waits following an exponential backoff algorithm

func (*Client) Delete

func (client *Client) Delete(path string, mods ...func(*Req)) (Res, error)

Delete makes a DELETE request.

func (*Client) Do

func (client *Client) Do(req Req) (Res, error)

Do makes a request. Requests for Do are built ouside of the client, e.g.

req := client.NewReq("GET", "/organizations", nil)
res, _ := client.Do(req)

func (*Client) Get

func (client *Client) Get(path string, mods ...func(*Req)) (Res, error)

Get makes a GET request and returns a GJSON result. It handles pagination transparently. Results will be the raw data structure as returned by Meraki API

func (Client) NewReq

func (client Client) NewReq(method, uri string, body io.Reader, mods ...func(*Req)) Req

NewReq creates a new Req request for this client.

func (*Client) Post

func (client *Client) Post(path, data string, mods ...func(*Req)) (Res, error)

Post makes a POST request and returns a GJSON result. Hint: Use the Body struct to easily create POST body data.

func (*Client) Put

func (client *Client) Put(path, data string, mods ...func(*Req)) (Res, error)

Put makes a PUT request and returns a GJSON result. Hint: Use the Body struct to easily create PUT body data.

type Req

type Req struct {
	// HttpReq is the *http.Request obejct.
	HttpReq *http.Request
	// LogPayload indicates whether logging of payloads should be enabled.
	LogPayload bool
}

Req wraps http.Request for API requests.

type Res

type Res struct {
	gjson.Result
	Header http.Header
}

Res is an API response returned by client requests. This is a GJSON result, which offers advanced and safe parsing capabilities. https://github.com/tidwall/gjson

Jump to

Keyboard shortcuts

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