GithubHelp home page GithubHelp logo

3scale-porta-go-client's Introduction

3scale-porta-go-client

CircleCI License GitHub release

3scale Account Management API Client

Installation

To install, run:

$ go get github.com/3scale/3scale-porta-go-client/client

And import using:

import "github.com/3scale/3scale-porta-go-client/client"

Usage

Basic usage

package main

import (
	"encoding/json"
	"fmt"

	"github.com/3scale/3scale-porta-go-client/client"
)

func jsonPrint(obj interface{}) {
	jsonData, err := json.MarshalIndent(obj, "", "  ")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(jsonData))
}

func main() {
	adminPortalURL := "https://3scale-admin.example.com"
	threescaleAccessToken := "************************"

	adminPortal, err := client.NewAdminPortalFromStr(adminPortalURL)
	if err != nil {
		fmt.Printf("%+v\n", err)
		return
	}

	threescaleClient := client.NewThreeScale(adminPortal, threescaleAccessToken, nil)

	backendList, err := threescaleClient.ListBackendApis()
	if err != nil {
		fmt.Printf("%+v\n", err)
		return
	}

	jsonPrint(backendList)
}

The client.NewThreeScale constructor allows injecting you own customized *http.Client.

In this example, the customized transport will log all http requests and responses

type DebuggerTransport struct {
	Transport http.RoundTripper
}

// RoundTrip is the core part of this module and implements http.RoundTripper.
// Executes HTTP request with request/response logging.
func (t *DebuggerTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	t.logRequest(req)

	resp, err := t.transport().RoundTrip(req)
	if err != nil {
		return resp, err
	}

	t.logResponse(resp)

	return resp, err
}

func (t *DebuggerTransport) logRequest(req *http.Request) {
	dump, _ := httputil.DumpRequestOut(req, true)
	fmt.Println(string(dump))
}

func (t *DebuggerTransport) logResponse(resp *http.Response) {
	dump, _ := httputil.DumpResponse(resp, true)
	fmt.Println(string(dump))
}

func (t *DebuggerTransport) transport() http.RoundTripper {
	if t.Transport != nil {
		return t.Transport
	}

	return http.DefaultTransport
}

var transport http.RoundTripper = &DebuggerTransport{}

threescaleClient := client.NewThreeScale(adminPortal, threescaleAccessToken, &http.Client{Transport: transport})

In this example, the customized transport allow insecure TLS connections (warning: this use is not recommended)

var transport http.RoundTripper = &http.Transport{
	TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

threescaleClient := client.NewThreeScale(adminPortal, threescaleAccessToken, &http.Client{Transport: transport})

Development

Testing

$ make test

Optionally, add TEST_NAME makefile variable to run specific test

make test TEST_NAME=TestActivateUserErrors

or even subtest

make test TEST_NAME=TestActivateUserErrors/UnexpectedHTTPStatusCode

Contributing

Bug reports and pull requests are welcome on GitHub

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.