ipapi

package module
v0.0.0-...-f239ff4 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2022 License: MIT Imports: 6 Imported by: 0

README

ipapi

Query https://ip-api.com including rate limit handling. The base usage of this library is for low request applications without a paid https://ip-api.com account. It is though possible to configure the library for usage with a paid account.

This is not an official library for https://ip-api.com.

Installation

Install into your project using go get github.com/cmol/ipapi.

Usage

The library works by returning a channel containing the result.

c, err := ipapi.Lookup("8.8.8.8")
if err != nil {
  // handle error
}
response := ipapi.Response{}
select {
  case response = <-c:
    if c.Status == "fail" {
      // do something with c.message
      return
    }
    // do something with the response object
  case default:
    // channel was closed, handle error
}

You can set the fields requested via ipapi.Fields. These fields can be configured using https://ip-api.com/docs/api:json

The default is:

var Fields = "?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,query"

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Similar libraries

A similar library exists at: https://pkg.go.dev/github.com/BenB196/ip-api-go-pkg

License

MIT

Documentation

Overview

Package ipapi allows for easy fetching of IP data, while still retaining the rate limiting specified

Index

Constants

This section is empty.

Variables

View Source
var APIKey = ""

APIKey holds the key for a paying customer. Blank by default

View Source
var Endpoint = "http://ip-api.com/json/"

Endpoint is the query location for all queries

View Source
var Fields = "?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,query"

Fields configures what to query from the API. This can be either as a comma separated string or the string of the numeric value eg. "61439". For full documentation see: https://ip-api.com/docs/api:json Note that

View Source
var MaxQueueLength = 50

MaxQueueLength limits the total number of elements in the queue. This is only relevant for rate limited usage.

View Source
var TTLBuffer = 5

TTLBuffer is added wait time for the API to let its TTL reach zero

Functions

func Lookup

func Lookup(address string) (chan Response, error)

Lookup adds query to queue and returns the result channel

Types

type Response

type Response struct {
	Query         string   `json:"query,omitempty"`
	Status        string   `json:"status,omitempty"`
	Message       string   `json:"message,omitempty"`
	Continent     string   `json:"continent,omitempty"`
	ContinentCode string   `json:"continentCode,omitempty"`
	Country       string   `json:"country,omitempty"`
	CountryCode   string   `json:"countryCode,omitempty"`
	Region        string   `json:"region,omitempty"`
	RegionName    string   `json:"regionName,omitempty"`
	City          string   `json:"city,omitempty"`
	District      string   `json:"district,omitempty"`
	ZIP           string   `json:"zip,omitempty"`
	Latitude      *float64 `json:"lat,omitempty"`
	Longtitude    *float64 `json:"lon,omitempty"`
	Timezone      string   `json:"timezone,omitempty"`
	Offset        *int64   `json:"offset,omitempty"`
	Currency      string   `json:"currency,omitempty"`
	ISP           string   `json:"isp,omitempty"`
	Organization  string   `json:"org,omitempty"`
	AS            string   `json:"as,omitempty"`
	ASName        string   `json:"asname,omitempty"`
	Reverse       string   `json:"reverse,omitempty"`
	Mobile        *bool    `json:"mobile,omitempty"`
	Proxy         *bool    `json:"proxy,omitempty"`
	Hosting       *bool    `json:"hosting,omitempty"`
}

Response holds data for each of the possible data points

Jump to

Keyboard shortcuts

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