antegral / netfunnel-go

A wrapper library of NetFunnel for Go

Home Page:https://pkg.go.dev/github.com/antegral/netfunnel-go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NetFunnel-Go

A wrapper library of NetFunnel for Go

Before use

Warning

NetFunnel API runs in a separate environment for each service you use, so you need to know in advance the API Endpoint of the NetFunnel for the service you want to use.

Install

$ go get github.com/antegral/netfunnel-go

Example

import (
	"net/http"
	"net/http/cookiejar"
	"net/url"

	"github.com/antegral/netfunnel-go"
)

const NETFUNNEL_API = "https://netfunnel.example.com/ts.wseq"
const APPLICATION_API = "https://api.example.com"

func main() {
  // 1. Create a Client struct for NetFunnel.
  nf := Netfunnel.Client{
    ApiEndpoint: NETFUNNEL_API,
    RetryInterval: 1 * time.Second,
  }

  // 2. Get a valid ticket for NetFunnel.
  // If the queue is full, it will wait for a ticket to become valid.
  ticket := nf.GetTicket()

  // 3. Put the ticket into the cookie jar.
  jar, _ := cookiejar.New(nil)
  var cookies []*http.Cookie
  cookie := &http.Cookie{
    Name:  "NetFunnel_ID",
    Value: ticket.Id,
  }
  cookies = append(cookies, cookie)
  jar.SetCookies(&url.URL{Host: APPLICATION_API}, cookies)

  // 4. Now use it as you would normally use a cookie jar.
  client := http.Client{
    Jar: jar,
  }

  // 5. Done!
  client.Post(APPLICATION_API, "application/json", nil)
}

About

A wrapper library of NetFunnel for Go

https://pkg.go.dev/github.com/antegral/netfunnel-go

License:MIT License


Languages

Language:Go 100.0%