geoip2

package module
v0.0.0-...-9968b08 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2015 License: Apache-2.0 Imports: 4 Imported by: 20

README

geoip2

golang implementation of MaxMind's GeoIP2 Precision Services

Example

Simple example to use the maxmind realtime api to perform a geo-query. While City takes an optional context.Context parameter, you can pass in nil if you don't care to use this.

package main

import (
	"os"
	"encoding/json"

	"github.com/savaki/geoip2"
)

func main() {
	api := geoip2.New(os.Getenv("MAXMIND_USER_ID"), os.Getenv("MAXMIND_LICENSE_KEY"))
	resp, _ := api.City(nil, "1.2.3.4")
	json.NewEncoder(os.Stdout).Encode(resp)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Api

type Api struct {
	// contains filtered or unexported fields
}

func New

func New(userId, licenseKey string) *Api

func WithClient

func WithClient(api *Api, client *http.Client) *Api

func WithClientFunc

func WithClientFunc(api *Api, ctxFunc func(context.Context, *http.Request) (*http.Response, error)) *Api

func (*Api) City

func (a *Api) City(ctx context.Context, ipAddress string) (Response, error)
Example
package main

import (
	"encoding/json"
	"os"

	"github.com/savaki/geoip2"
)

func main() {
	userId := os.Getenv("MAXMIND_USER_ID")
	licenseKey := os.Getenv("MAXMIND_LICENSE_KEY")
	api := geoip2.New(userId, licenseKey)

	resp, _ := api.City(nil, "1.2.3.4")
	json.NewEncoder(os.Stdout).Encode(resp)
}
Output:

func (*Api) Country

func (a *Api) Country(ctx context.Context, ipAddress string) (Response, error)
Example
package main

import (
	"encoding/json"
	"os"
	"time"

	"github.com/savaki/geoip2"
	"golang.org/x/net/context"
)

func main() {
	userId := os.Getenv("MAXMIND_USER_ID")
	licenseKey := os.Getenv("MAXMIND_LICENSE_KEY")
	api := geoip2.New(userId, licenseKey)

	ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
	resp, _ := api.Country(ctx, "1.2.3.4")
	json.NewEncoder(os.Stdout).Encode(resp)
}
Output:

func (*Api) Insights

func (a *Api) Insights(ctx context.Context, ipAddress string) (Response, error)
Example
package main

import (
	"encoding/json"
	"os"
	"time"

	"github.com/savaki/geoip2"
	"golang.org/x/net/context"
)

func main() {
	userId := os.Getenv("MAXMIND_USER_ID")
	licenseKey := os.Getenv("MAXMIND_LICENSE_KEY")
	api := geoip2.New(userId, licenseKey)

	ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
	resp, _ := api.Insights(ctx, "1.2.3.4")
	json.NewEncoder(os.Stdout).Encode(resp)
}
Output:

type City

type City struct {
	Confidence int               `json:"confidence,omitempty"`
	GeoNameId  int               `json:"geoname_id,omitempty"`
	Names      map[string]string `json:"names,omitempty"`
}

type Continent

type Continent struct {
	Code      string            `json:"code,omitempty"`
	GeoNameId int               `json:"geoname_id,omitempty"`
	Names     map[string]string `json:"names,omitempty"`
}

type Country

type Country struct {
	Confidence int               `json:"confidence,omitempty"`
	GeoNameId  int               `json:"geoname_id,omitempty"`
	IsoCode    string            `json:"iso_code,omitempty"`
	Names      map[string]string `json:"names,omitempty"`
}

type Error

type Error struct {
	Code string `json:"code,omitempty"`
	Err  string `json:"error,omitempty"`
}

func (Error) Error

func (e Error) Error() string

type Location

type Location struct {
	AccuracyRadius    int     `json:"accuracy_radius,omitempty"`
	AverageIncome     int     `json:"average_income,omitempty"`
	Latitude          float64 `json:"latitude,omitempty"`
	Longitude         float64 `json:"longitude,omitempty"`
	MetroCode         int     `json:"metro_code,omitempty"`
	PopulationDensity int     `json:"population_density,omitempty"`
	TimeZone          string  `json:"time_zone,omitempty"`
}

type MaxMind

type MaxMind struct {
	QueriesRemaining int `json:"queries_remaining,omitempty"`
}

type Postal

type Postal struct {
	Code       string `json:"code,omitempty"`
	Confidence int    `json:"confidence,omitempty"`
}

type RegisteredCountry

type RegisteredCountry struct {
	GeoNameId int               `json:"geoname_id,omitempty"`
	IsoCode   string            `json:"iso_code,omitempty"`
	Names     map[string]string `json:"names,omitempty"`
}

type RepresentedCountry

type RepresentedCountry struct {
	GeoNameId int               `json:"geoname_id,omitempty"`
	IsoCode   string            `json:"iso_code,omitempty"`
	Names     map[string]string `json:"names,omitempty"`
	Type      string            `json:"type,omitempty"`
}

type Response

type Response struct {
	City               City               `json:"city,omitempty"`
	Continent          Continent          `json:"continent,omitempty"`
	Country            Country            `json:"country,omitempty"`
	Location           Location           `json:"location,omitempty"`
	Postal             Postal             `json:"postal,omitempty"`
	RegisteredCountry  RegisteredCountry  `json:"registered_country,omitempty"`
	RepresentedCountry RepresentedCountry `json:"represented_country,omitempty"`
	Subdivisions       []Subdivision      `json:"subdivisions,omitempty"`
	Traits             Traits             `json:"traits,omitempty"`
	MaxMind            MaxMind            `json:"maxmind,omitempty"`
}

type Subdivision

type Subdivision struct {
	Confidence int               `json:"confidence,omitempty"`
	GeoNameId  int               `json:"geoname_id,omitempty"`
	IsoCode    string            `json:"iso_code,omitempty"`
	Names      map[string]string `json:"names,omitempty"`
}

type Traits

type Traits struct {
	AutonomousSystemNumber       int    `json:"autonomous_system_number,omitempty"`
	AutonomousSystemOrganization string `json:"autonomous_system_organization,omitempty"`
	Domain                       string `json:"domain,omitempty"`
	IsAnonymousProxy             bool   `json:"is_anonymous_proxy,omitempty"`
	IsSatelliteProvider          bool   `json:"is_satellite_provider,omitempty"`
	Isp                          string `json:"isp,omitempty"`
	IpAddress                    string `json:"ip_address,omitempty"`
	Organization                 string `json:"organization,omitempty"`
	UserType                     string `json:"user_type,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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