provider

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 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
	providerA := listProviders[0]

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

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

	// Get the current global/public IP address
	ipAddress, err := providerA.GetIP()
	if err != nil {
		log.Println(err)

		return
	}

	fmt.Println(ipAddress.String())

}

//nolint:nonamedreturns // Allow named returns for readability
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
_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.
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.
ipifyorg
Package ipifyorg implements an interface to the ipify.org API.
Package ipifyorg implements an interface to the ipify.org API.
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.

Jump to

Keyboard shortcuts

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