frappe

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

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

Go to latest
Published: Sep 24, 2020 License: MIT Imports: 13 Imported by: 0

README

go-frappe-client

Installation

go get github.com/joeirimpan/go-frappe-client

Usage

Authentication

Login authentication
auth := LoginAuth{
	userName: "username",
	password: "password",
}
frappeClient, _ := frappe.New("http://localhost:5001/", &auth, true)
Basic authentication
auth := BasicAuth{
	apiKey: "api_key",
	apiSecret: "api_secret",
}
frappeClient, _ := frappe.New("http://localhost:5001/", &auth, true)
Token authentication
auth := TokenAuth{
	apiKey: "api_key",
	apiSecret: "api_secret",
}
frappeClient, _ := frappe.New("http://localhost:5001/", &auth, true)

Sample program

package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"

	frappe "github.com/joeirimpan/go-frappe-client"
)

// SampleResp holder struct
type SampleResp struct {
	Message string   `json:"message"`
}

func main() {
	auth := LoginAuth{
		userName: "username",
		password: "password",
	}
	frappeClient, _ := frappe.New("http://localhost:5001/", &auth, true)

	// Creating a post request
	s := SampleResp{}
	params := url.Values{}
	params.Set("param1", "value1")
	resp, _ := frappeClient.Do(http.MethodPost, "module.module.my_api_method", params, nil)
	if err := json.Unmarshal(resp.Body, &s); err != nil {
		fmt.Println(err)
	}
	fmt.Println(s)

	// Creating a post request and serialize back to struct
	r := SampleResp{}
	params = url.Values{}
	params.Set("param1", "value1")
	frappeClient.DoJSON(http.MethodPost, "module.module.my_api_method", params, nil, &r)
	fmt.Println(r)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

type Auth interface{}

Auth generic auth interface

type BasicAuth

type BasicAuth struct {
	APIKey    string
	APISecret string
}

BasicAuth sends base64 encoded auth header

type Client

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

Client refers to frappe client

func New

func New(baseURI string, auth Auth, debug bool) (*Client, error)

New creates a new frappe client.

func (*Client) Do

func (c *Client) Do(httpMethod, frappeMethod string, params url.Values, headers http.Header) (HTTPResponse, error)

Do proxy underlying http client do request

func (*Client) DoJSON

func (c *Client) DoJSON(httpMethod, frappeMethod string, params url.Values, headers http.Header, obj interface{}) (HTTPResponse, error)

DoJSON proxy underlying http client doJSON request

func (*Client) DoRaw

func (c *Client) DoRaw(httpMethod, frappeMethod string, reqBody []byte, headers http.Header) (HTTPResponse, error)

DoRaw proxy underlying http client do request

func (*Client) Login

func (c *Client) Login() error

Login performs a login request and sets the cookies

func (*Client) SetHTTPClient

func (c *Client) SetHTTPClient(h *http.Client)

SetHTTPClient sets http client for frappe client

type HTTPClient

type HTTPClient interface {
	Do(method, rURL string, params url.Values, headers http.Header) (HTTPResponse, error)
	DoRaw(method, rURL string, reqBody []byte, headers http.Header) (HTTPResponse, error)
	DoJSON(method, rURL string, params url.Values, headers http.Header, obj interface{}) (HTTPResponse, error)
	GetClient() *httpClient
}

HTTPClient represents an HTTP client.

func NewHTTPClient

func NewHTTPClient(h *http.Client, hLog *log.Logger, debug bool) HTTPClient

NewHTTPClient returns a self-contained HTTP request object with underlying keep-alive transport.

type HTTPResponse

type HTTPResponse struct {
	Body     []byte
	Response *http.Response
}

HTTPResponse encompasses byte body + the response of an HTTP request.

type LoginAuth

type LoginAuth struct {
	UserName string
	Password string
}

LoginAuth performs normal login flow

type TokenAuth

type TokenAuth struct {
	APIKey    string
	APISecret string
}

TokenAuth sends token formed from apiKey and apiSecret

Jump to

Keyboard shortcuts

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