pay

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2018 License: MPL-2.0 Imports: 3 Imported by: 0

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

func NewClient(httpClient *http.Client, lnClient LNclient) Client

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

func (c *Client) Do(req *http.Request) (*http.Response, error)

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

func (c *Client) Get(url string) (*http.Response, error)

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.

type LNclient added in v0.3.0

type LNclient interface {
	// Pay pays the invoice and returns the preimage on success, or an error on failure.
	Pay(invoice string) (string, error)
}

LNclient is the abstraction of a Lightning Network node client for paying LN invoices.

Jump to

Keyboard shortcuts

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