provider

package
v1.0.1-beta2 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Provider

type Provider interface {
	// GetIP returns the global/public IP address of the current machine.
	GetIP() (net.IP, error)
	// SetURL overrides the default value of the API endpoint URL.
	SetURL(url string)
	// Name returns the current providers URL.
	Name() string
}

Provider is the interface which each provider package must implement.

func GetAll

func GetAll() []Provider

GetAll returns all providers.

Note that if you implement a new provider, you must add it in this function.

Example
package main

import (
	"fmt"
	"log"
	"net/http"
	"net/http/httptest"

	"github.com/KEINOS/whereami/pkg/provider"
)

func main() {
	listProviders := provider.GetAll()

	// Use the 1st provider
	p := listProviders[0]

	// To avoid unnecessary API requests during testing the example, the URL is
	// temporarily set to a dummy server. This server returns "123.123.123.123".
	dummyURL, closer := getDummyServerURL()
	defer closer()

	p.SetURL(dummyURL) // Override the default API endpoint URL.

	// Get the current global/public IP address
	ip, err := p.GetIP()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(ip.String())

}

func getDummyServerURL() (dummyURL string, deferFn func()) {
	dummySrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		if _, err := w.Write([]byte(`{"ip": "123.123.123.123"}`)); err != nil {
			log.Fatalf("dummy server creation failed during test. Error: %v", err)
		}
	}))

	return dummySrv.URL, dummySrv.Close
}
Output:

123.123.123.123

Directories

Path Synopsis
providers
inetcluecom
Package inetcluecom provides an interface to the inetclue.com web service.
Package inetcluecom provides an interface to the inetclue.com web service.
inetipinfo
Package inetipinfo provides an interface to the inet-ip.info.
Package inetipinfo provides an interface to the inet-ip.info.
ipinfoio
Package ipinfoio provides an interface to the ipinfo.io API.
Package ipinfoio provides an interface to the ipinfo.io API.
toolpageorg
Package toolpageorg provides an interface to the en.toolpage.org web service.
Package toolpageorg provides an interface to the en.toolpage.org web service.
whatismyipcom
Package whatismyipcom provides an interface to the www.whatismyip.com web service.
Package whatismyipcom provides an interface to the www.whatismyip.com web service.

Jump to

Keyboard shortcuts

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