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 ¶
- func CheckResponse(r *http.Response) error
- func GetCity(ip net.IP) (string, error)
- func GetCountry(ip net.IP) (string, error)
- func GetHostname(ip net.IP) (string, error)
- func GetIP(ip net.IP) (net.IP, error)
- func GetLocation(ip net.IP) (string, error)
- func GetOrganization(ip net.IP) (string, error)
- func GetPhone(ip net.IP) (string, error)
- func GetPostal(ip net.IP) (string, error)
- func GetRegion(ip net.IP) (string, error)
- func SetCache(cache *Cache)
- type ASNInfo
- type ASNPrefix
- type AuthTransport
- type Cache
- type Client
- func (c *Client) ASN(asn string) (*ASNInfo, error)
- func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error)
- func (c *Client) GetCity(ip net.IP) (string, error)
- func (c *Client) GetCountry(ip net.IP) (string, error)
- func (c *Client) GetGeo(ip net.IP) (*Geo, error)
- func (c *Client) GetHostname(ip net.IP) (string, error)
- func (c *Client) GetIP(ip net.IP) (net.IP, error)
- func (c *Client) GetInfo(ip net.IP) (*Info, error)
- func (c *Client) GetLocation(ip net.IP) (string, error)
- func (c *Client) GetOrganization(ip net.IP) (string, error)
- func (c *Client) GetPhone(ip net.IP) (string, error)
- func (c *Client) GetPostal(ip net.IP) (string, error)
- func (c *Client) GetRegion(ip net.IP) (string, error)
- func (c *Client) NewRequest(urlStr string) (*http.Request, error)
- func (c *Client) SetCache(cache *Cache)
- type ErrorResponse
- type EvaluatorFunc
- type Geo
- type Info
- type InvalidASNError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckResponse ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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.
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.
type 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 NewClient ¶
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) Do ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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.
type InvalidASNError ¶
type InvalidASNError struct {
ASN string
}
InvalidASNError is reported when the invalid ASN was specified.
func (*InvalidASNError) Error ¶
func (err *InvalidASNError) Error() string