restapi

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

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

Go to latest
Published: Mar 13, 2025 License: MIT Imports: 13 Imported by: 10

README

Simple restapi handling tool

Created to do simple restapi calls in go. There are probably better tool :)

Example

Using Tesla APi to get a list of vechiles

  vehicleList := restapi.NewGet("vehicles", https://owner-api.teslamotors.com/api/1/vehicles)


  vehicleList.SetBearerAccessToken("AccessTokenFromTheAuthenticationAPI")
  vehicleList.HasInnerMapArray("response","count")

  if(vehicleList.Send()){
    vehicleList.Dump()
  }else{
    fmt.Println("get vehicles list failed")
  }

  count := vehicleList.GetValueInt("count")

  fmt.Printf("Number of vehicles[%d]\n",count)

  for j:= 0; j < count; j++ {
    fmt.Println("Vehicle: ", j)
    fmt.Printf("Ids[%s]\n", vehicleList.GetArrayValueString(j,"id_s"))
    fmt.Printf("Vin[%s]\n", vehicleList.GetArrayValueString(j,"vin"))
    fmt.Printf("DisplayName[%s]\n", vehicleList.GetArrayValueString(j,"display_name"))
    fmt.Printf("State[%s]\n", vehicleList.GetArrayValueString(j,"state"))

  }

08/22/2022 - Updated

Will put code example in soon. Updated to include the ability to save a json response to a go struct auto-magically for building your application

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CastArray

func CastArray(item interface{}) []interface{}

func CastFloatToInt

func CastFloatToInt(item interface{}) int

func CastFloatToInt64

func CastFloatToInt64(item interface{}) uint64

func CastMap

func CastMap(item interface{}) map[string]interface{}

func CastString

func CastString(item interface{}) string

func TurnOffCertValidation

func TurnOffCertValidation()

func TurnOnCertValidation

func TurnOnCertValidation()

Types

type HttpMethod

type HttpMethod int
const (
	Get HttpMethod = 1 + iota
	Post
	Put
	Delete
)

type Restapi

type Restapi struct {
	Method HttpMethod

	RawData    interface{} // used to contain the raw response msg mody
	BodyString string
	BodyBytes  []byte
	// contains filtered or unexported fields
}

func New

func New(method HttpMethod, name string, url string) *Restapi

func NewDelete

func NewDelete(name string, url string) *Restapi

func NewGet

func NewGet(name string, url string) *Restapi

func NewGetXML

func NewGetXML(name string, url string, parseresponse bool) *Restapi

func NewPost

func NewPost(name string, url string) *Restapi

func NewPut

func NewPut(name string, url string) *Restapi

func (*Restapi) DebugOff

func (pRA *Restapi) DebugOff()

func (*Restapi) DebugOn

func (pRA *Restapi) DebugOn()

func (*Restapi) Dump

func (pRA *Restapi) Dump()

func (*Restapi) FetchTLSCert

func (pRA *Restapi) FetchTLSCert(url string) (*x509.Certificate, bool)

func (*Restapi) GetArrayValue

func (pRA *Restapi) GetArrayValue(index int, key string) interface{}

func (*Restapi) GetArrayValueInt

func (pRA *Restapi) GetArrayValueInt(index int, key string) int

func (*Restapi) GetArrayValueInt64

func (pRA *Restapi) GetArrayValueInt64(index int, key string) uint64

func (*Restapi) GetArrayValueString

func (pRA *Restapi) GetArrayValueString(index int, key string) string

func (*Restapi) GetLastStatusCode

func (pRA *Restapi) GetLastStatusCode() int

func (*Restapi) GetName

func (pRA *Restapi) GetName() string

func (*Restapi) GetResponseBody

func (pRA *Restapi) GetResponseBody() string

func (*Restapi) GetUrl

func (pRA *Restapi) GetUrl() string

func (*Restapi) GetValue

func (pRA *Restapi) GetValue(index string) interface{}

func (*Restapi) GetValueInt

func (pRA *Restapi) GetValueInt(index string) int

func (*Restapi) GetValueString

func (pRA *Restapi) GetValueString(index string) string

func (*Restapi) HasInnerMap

func (pRA *Restapi) HasInnerMap(name string)

func (*Restapi) HasInnerMapArray

func (pRA *Restapi) HasInnerMapArray(name string, countname string)

func (*Restapi) JsonOnly

func (pRA *Restapi) JsonOnly()

func (*Restapi) SaveResponseBody

func (pRA *Restapi) SaveResponseBody(filename string, structname string, bstdout bool) bool

func (*Restapi) Send

func (pRA *Restapi) Send() bool

func (*Restapi) SetApiKey

func (pRA *Restapi) SetApiKey(ApiKey string)

func (*Restapi) SetBasicAccessToken

func (pRA *Restapi) SetBasicAccessToken(AccessToken string)

func (*Restapi) SetBearerAccessToken

func (pRA *Restapi) SetBearerAccessToken(AccessToken string)

func (*Restapi) SetPostJson

func (pRA *Restapi) SetPostJson(jsonstr string) bool

func (*Restapi) UseCert

func (pRA *Restapi) UseCert(certfile string) bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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