kiteauth

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: MIT Imports: 11 Imported by: 0

README

Kite API Authentication Client

The Go client to generate user tokens automatically for using with customised version of KiteConnect and Kiteticker API.

Usage Instructions

Required:
  • userId : Your kite user_id
  • password : Your kite password
  • totpSecret : Its a value which you can copy while setting your external 2FA TOTP
Obtaining 2FA TOTP Secret
  • Set up External 2FA TOTP Auth by going to "My Profile > Settings > Account Security > External 2FA TOTP" and copy the value, while setting.

Installation

go get github.com/nsvirk/gokiteauth

Sample code

package main

import (
	"fmt"

	kiteauth "github.com/nsvirk/gokiteauth"
)

const (
	userId      string = "my_user_id"
	password    string = "my_password"
	totpSecret  string = "my_totp_secret"
)

func main() {
	// Create a new Kite auth instance
	ka := kiteauth.New(userId)
	// ka.SetDebug(true)

	// Generate user session
	userSession, err := ka.GenerateSession(password, totpSecret)
	if err != nil {
		fmt.Printf("Error: %v", err)
		return
	}

	fmt.Println("----------------------------------------------------")
	fmt.Println("** USER SESSION **: ")
	fmt.Println("----------------------------------------------------")
	fmt.Println("UserID		: ", userSession.UserID)
	fmt.Println("Enctoken		: ", userSession.Enctoken)
	fmt.Println("LoginTime		: ", userSession.LoginTime)
	fmt.Println("----------------------------------------------------")

	// Set enctoken
	ka.SetEnctoken(userSession.Enctoken)

	// Get user profile
	userprofile, err := ka.GetUserProfile()
	if err != nil {
		fmt.Printf("Error: %v", err)
		return
	}
	fmt.Println("----------------------------------------------------")
	fmt.Println("** USER PROFILE **")
	fmt.Println("----------------------------------------------------")
	fmt.Println("UserID		: ", userprofile.UserID)
	fmt.Println("UserType		: ", userprofile.UserType)
	fmt.Println("UserName		: ", userprofile.UserName)
	fmt.Println("UserShortname	: ", userprofile.UserShortname)
	fmt.Println("Email		: ", userprofile.Email)
	fmt.Println("Broker		: ", userprofile.Broker)
	fmt.Println("Exchanges		: ", userprofile.Exchanges)
	fmt.Println("Products		: ", userprofile.Products)
	fmt.Println("OrderTypes		: ", userprofile.OrderTypes)
	fmt.Println("AvatarURL		: ", userprofile.AvatarURL)
	fmt.Println("Meta		: ", userprofile.Meta)
	fmt.Println("----------------------------------------------------")
}

Documentation

Index

Constants

View Source
const (
	URILogin       string = "/api/login"
	URITwofa       string = "/api/twofa"
	URIUserProfile string = "/oms/user/profile"
)

API endpoints

View Source
const (
	GeneralError    = "GeneralException"
	TokenError      = "TokenException"
	PermissionError = "PermissionError"
	UserError       = "UserException"
	TwoFAError      = "TwoFAException"
	OrderError      = "OrderException"
	InputError      = "InputException"
	DataError       = "DataException"
	NetworkError    = "NetworkException"
)

API errors.

Variables

This section is empty.

Functions

func GetErrorName

func GetErrorName(code int) string

GetErrorName returns an error name given an HTTP code.

func NewError

func NewError(etype string, message string, data interface{}) error

NewError creates and returns a new instace of Error with custom error metadata.

Types

type Client

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

Client represents interface for Kiteauth client.

func New

func New(userId string) *Client

New creates a new Kiteauth client.

func (*Client) GenerateSession

func (c *Client) GenerateSession(password, totpSecret string) (UserSession, error)

GenerateSession gets a user session Step 1: First a login request is made and request_id is obtained. Step 2: Then a twofa request is made to generate the user session.

func (*Client) GetUserProfile

func (c *Client) GetUserProfile() (UserProfile, error)

GetUserProfile gets user profile.

func (*Client) SetBaseURI

func (c *Client) SetBaseURI(baseURI string)

SetBaseURI overrides the base Kiteauth API endpoint with custom url.

func (*Client) SetDebug

func (c *Client) SetDebug(debug bool)

SetDebug sets debug mode to enable HTTP logs.

func (*Client) SetEnctoken

func (c *Client) SetEnctoken(enctoken string)

SetEnctoken sets the enctoken to the Kiteauth instance.

func (*Client) SetHTTPClient

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

SetHTTPClient overrides default http handler with a custom one. This can be used to set custom timeouts and transport.

func (*Client) SetTimeout

func (c *Client) SetTimeout(timeout time.Duration)

SetTimeout sets request timeout for default http client.

type Error

type Error struct {
	Code      int
	ErrorType string
	Message   string
	Data      interface{}
}

Error is the error type used for all API errors.

func (Error) Error

func (e Error) Error() string

This makes Error a valid Go error type.

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)
	DoEnvelope(method, url string, params url.Values, headers http.Header, obj interface{}) error
	DoJSON(method, url 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 LoginResonse

type LoginResonse struct {
	UserId      string   `json:"user_id"`
	RequestId   string   `json:"request_id"`
	TwofaType   string   `json:"twofa_type"`
	TwofaTypes  []string `json:"twofa_types"`
	TwofaStatus string   `json:"twofa_status"`
	Profile     struct {
		UserName      string `json:"user_name"`
		UserShortname string `json:"user_shortname"`
		AvatarURL     any    `json:"avatar_url"`
	} `json:"profile"`
}

LoginResonse represents a login response

type PlainResponse

type PlainResponse struct {
	Code    int    `json:"code"`
	Message string `json:"string"`
}

PlainResponse is a helper for receiving blank HTTP envelop responses without any payloads.

type Time

type Time struct {
	time.Time
}

Time represents a time

type UserProfile

type UserProfile struct {
	UserId        string      `json:"user_id"`
	UserType      string      `json:"user_type"`
	Email         string      `json:"email"`
	UserName      string      `json:"user_name"`
	UserShortname string      `json:"user_shortname"`
	Broker        string      `json:"broker"`
	Exchanges     []string    `json:"exchanges"`
	Products      []string    `json:"products"`
	OrderTypes    []string    `json:"order_types"`
	AvatarURL     interface{} `json:"avatar_url"`
	Meta          struct {
		DematConsent string `json:"demat_consent"`
	} `json:"meta"`
}

UserProfile represents a user profile.

type UserSession

type UserSession struct {
	UserId    string `json:"user_id"`
	Enctoken  string `json:"enctoken"`
	LoginTime string `json:"login_time"`
}

UserSession represents a user session

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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