Documentation ¶
Overview ¶
Package pay contains the client-side functionality for easy use of APIs that are paywalled with ln-paywall or other compatible paywall implementations.
Usage
package main import ( "fmt" "io/ioutil" "github.com/philippgille/ln-paywall/ln" "github.com/philippgille/ln-paywall/pay" ) func main() { // Set up client lndOptions := ln.LNDoptions{ // Default address: "localhost:10009", CertFile: "tls.cert" MacaroonFile: "admin.macaroon", // admin.macaroon is required for making payments } lnClient, err := ln.NewLNDclient(lndOptions) if err != nil { panic(err) } client := pay.NewClient(nil, lnClient) // Uses http.DefaultClient if no http.Client is passed // Send request to an ln-paywalled API res, err := client.Get("http://localhost:8080/ping") if err != nil { panic(err) } defer res.Body.Close() // Print response body resBody, err := ioutil.ReadAll(res.Body) if err != nil { panic(err) } fmt.Println(string(resBody)) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶ added in v0.5.0
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTP client, which handles "Payment Required" interruptions transparently. It must be initially set up with a connection the Lightning Network node that should handle the payments and from then on it's meant to be used as an alternative to the "net/http.Client". The calling code only needs to call the Do(...) method once, instead of handling "402 Payment Required" responses and re-sending the original request after payment.
func NewClient ¶ added in v0.5.0
NewClient creates a new pay.Client instance. You can pass nil as httpClient, in which case the http.DefaultClient will be used.
func (*Client) Do ¶ added in v0.5.0
Do sends the given request and automatically handles the required payment in the background. It does this by sending its own request to the URL + path of the given request to trigger a "402 Payment Required" response with an invoice. It then pays the invoice via the configured Lightning Network node. Finally it sends the originally intended (given) request with an additional HTTP header and returns the response.
func (*Client) Get ¶ added in v0.5.0
Get sends an HTTP GET request to the given URL and automatically handles the required payment in the background. It does this by sending its own request to the URL + path of the given request to trigger a "402 Payment Required" response with an invoice. It then pays the invoice via the configured Lightning Network node. Finally it sends the originally intended (given) request with an additional HTTP header and returns the response.