ipinfo

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2020 License: Apache-2.0 Imports: 11 Imported by: 26

Documentation

Overview

Package ipinfo provides a client for using the IPinfo API.

Usage:

import "github.com/ipinfo/go-ipinfo/ipinfo"

The default IPinfo client is predefined and can be used without initialization. For example:

info, err := ipinfo.GetInfo(net.ParseIP("8.8.8.8"))

Authentication

To perform authenticated API calls construct a new IPinfo client using AuthTransport HTTP client. For example:

authTransport := ipinfo.AuthTransport{Token: "MY_TOKEN"}
httpClient := authTransport.Client()
client := ipinfo.NewClient(httpClient)
info, err := client.GetInfo(net.ParseIP("8.8.8.8"))

Note that when using an authenticated Client, all calls made by the client will include the specified token.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckResponse

func CheckResponse(r *http.Response) error

CheckResponse checks the API response for errors, and returns them if present. A response is considered an error if it has a status code outside the 200 range.

func GetCity

func GetCity(ip net.IP) (string, error)

GetCity returns a specific field "city" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func GetCountry

func GetCountry(ip net.IP) (string, error)

GetCountry returns a specific field "country" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func GetHostname

func GetHostname(ip net.IP) (string, error)

GetHostname returns a specific field "hostname" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func GetIP

func GetIP(ip net.IP) (net.IP, error)

GetIP returns a specific field "ip" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func GetLocation

func GetLocation(ip net.IP) (string, error)

GetLocation returns a specific field "loc" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func GetOrganization

func GetOrganization(ip net.IP) (string, error)

GetOrganization returns a specific field "org" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func GetPhone

func GetPhone(ip net.IP) (string, error)

GetPhone returns a specific field "phone" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func GetPostal

func GetPostal(ip net.IP) (string, error)

GetPostal returns a specific field "postal" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func GetRegion

func GetRegion(ip net.IP) (string, error)

GetRegion returns a specific field "region" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func SetCache

func SetCache(cache *Cache)

Types

type ASNInfo

type ASNInfo struct {
	ASN         string      `json:"asn"`
	Name        string      `json:"name"`
	Country     string      `json:"country"`
	Allocated   string      `json:"allocated"`
	Registry    string      `json:"registry"`
	Domain      string      `json:"domain"`
	NumberOfIPs uint64      `json:"num_ips"`
	Type        string      `json:"type"`
	Prefixes    []ASNPrefix `json:"prefixes"`
	Prefixes6   []ASNPrefix `json:"prefixes6"`
}

ASNInfo represents ASN details.

func ASN

func ASN(asn string) (*ASNInfo, error)

ASN returns the details for the specified ASN.

type ASNPrefix

type ASNPrefix struct {
	Netblock string `json:"netblock"`
	ID       string `json:"id"`
	Name     string `json:"name"`
	Country  string `json:"country"`
}

ASNPrefix represents an ASN prefix.

type AuthTransport

type AuthTransport struct {
	Token     string
	Transport http.RoundTripper
}

AuthTransport is an http.RoundTripper that authenticates all requests with a token query string.

func (*AuthTransport) Client

func (t *AuthTransport) Client() *http.Client

Client returns an *http.Client that makes requests that are authenticated with a token query string.

func (*AuthTransport) RoundTrip

func (t *AuthTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface.

type Cache

type Cache struct {
	cache.Interface
	// contains filtered or unexported fields
}

func NewCache

func NewCache(engine cache.Interface) *Cache

func (*Cache) GetOrRequest

func (c *Cache) GetOrRequest(key string, evaluator EvaluatorFunc) (interface{}, error)

type Client

type Client struct {

	// Base URL for API requests. BaseURL should always be specified with a
	// trailing slash.
	BaseURL *url.URL

	// User agent used when communicating with the IPinfo API.
	UserAgent string

	// Cache interface implementation to prevent API quota overuse for
	// identical requests.
	Cache *Cache
	// contains filtered or unexported fields
}

A Client manages communication with IPinfo API.

func GetClient

func GetClient() *Client

GetClient get the global Client instance.

func NewClient

func NewClient(httpClient *http.Client) *Client

NewClient returns a new IPinfo API client. If a nil httpClient is provided, http.DefaultClient will be used. To use authenticated API methods provide http.Client with AuthTransport.

func (*Client) ASN

func (c *Client) ASN(asn string) (*ASNInfo, error)

ASN returns the details for the specified ASN.

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error)

Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface, the raw response body will be written to v, without attempting to first decode it.

func (*Client) GetCity

func (c *Client) GetCity(ip net.IP) (string, error)

GetCity returns a specific field "city" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func (*Client) GetCountry

func (c *Client) GetCountry(ip net.IP) (string, error)

GetCountry returns a specific field "country" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func (*Client) GetGeo

func (c *Client) GetGeo(ip net.IP) (*Geo, error)

GetGeo returns geolocation information for the specified IP. If nil was provieded instead of ip, it returns details for the caller's own IP.

func (*Client) GetHostname

func (c *Client) GetHostname(ip net.IP) (string, error)

GetHostname returns a specific field "hostname" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func (*Client) GetIP

func (c *Client) GetIP(ip net.IP) (net.IP, error)

GetIP returns a specific field "ip" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func (*Client) GetInfo

func (c *Client) GetInfo(ip net.IP) (*Info, error)

GetInfo returns full details for the specified IP. If nil was provieded instead of ip, it returns details for the caller's own IP.

func (*Client) GetLocation

func (c *Client) GetLocation(ip net.IP) (string, error)

GetLocation returns a specific field "loc" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func (*Client) GetOrganization

func (c *Client) GetOrganization(ip net.IP) (string, error)

GetOrganization returns a specific field "org" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func (*Client) GetPhone

func (c *Client) GetPhone(ip net.IP) (string, error)

GetPhone returns a specific field "phone" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func (*Client) GetPostal

func (c *Client) GetPostal(ip net.IP) (string, error)

GetPostal returns a specific field "postal" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func (*Client) GetRegion

func (c *Client) GetRegion(ip net.IP) (string, error)

GetRegion returns a specific field "region" value from the API for the provided ip. If nil was provided instead of ip, it returns details for the caller's own IP.

func (*Client) NewRequest

func (c *Client) NewRequest(urlStr string) (*http.Request, error)

NewRequest creates an API request. A relative URL can be provided in urlStr, in which case it is resolved relative to the BaseURL of the Client. Relative URLs should always be specified without a preceding slash.

func (*Client) SetCache

func (c *Client) SetCache(cache *Cache)

type ErrorResponse

type ErrorResponse struct {
	Response *http.Response // HTTP response that caused this error
	Err      struct {
		Title   string `json:"title"`
		Message string `json:"message"`
	} `json:"error"`
}

An ErrorResponse reports an error caused by an API request.

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

type EvaluatorFunc

type EvaluatorFunc func() (interface{}, error)

type Geo

type Geo struct {
	IP       net.IP `json:"ip"`
	City     string `json:"city"`
	Region   string `json:"region"`
	Country  string `json:"country"`
	Location string `json:"loc"`
	Phone    string `json:"phone"`
	Postal   string `json:"postal"`
}

Geo represents IP geolocation information.

func GetGeo

func GetGeo(ip net.IP) (*Geo, error)

GetGeo returns geolocation information for the specified IP. If nil was provieded instead of ip, it returns details for the caller's own IP.

type Info

type Info struct {
	Geo
	Hostname     string `json:"hostname"`
	Organization string `json:"org"`
}

Info represents full IP details.

func GetInfo

func GetInfo(ip net.IP) (*Info, error)

GetInfo returns full details for the specified IP. If nil was provieded instead of ip, it returns details for the caller's own IP.

type InvalidASNError

type InvalidASNError struct {
	ASN string
}

InvalidASNError is reported when the invalid ASN was specified.

func (*InvalidASNError) Error

func (err *InvalidASNError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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