Giter Site home page Giter Site logo

socialsky-io / go-freegeoip Goto Github PK

View Code? Open in Web Editor NEW

This project forked from shivam010/go-freegeoip

0.0 2.0 0.0 50 KB

Golang client for Free IP Geolocation API with inbuilt cache support to overcome the 15k per hour rate limit

License: Apache License 2.0

Makefile 1.18% Go 98.82%

go-freegeoip's Introduction

go-freeGeoIP client with inbuilt cache

Build Tests & Check Go Report Card GoDoc License GitHub release Coverage Status

go-freeGeoIP is a Golang client for Free IP Geolocation information API with inbuilt cache support to increase the 15k per hour rate limit of the application https://freegeoip.app/

By default, the client will cache the IP Geolocation information for 24 hours, but the expiry can be set manually. If you want set the information cache with no expiration time set the expiry function to nil.

A 24-hour cache expiry will be sufficient overcome the 15k per hour limit.

Installation

go get github.com/Shivam010/go-freeGeoIP

FreeGeoIP.app description

freegeoip.app provides a free IP geolocation API for software developers. It uses a database of IP addresses that are associated to cities along with other relevant information like time zone, latitude and longitude.

You're allowed up to 15,000 queries per hour by default. Once this limit is reached, all of your requests will result in HTTP 403, forbidden, until your quota is cleared.

The HTTP API takes GET requests in the following schema:

https://freegeoip.app/{format}/{IP_or_hostname}

Supported formats are: csv, xml, json and jsonp. If no IP or hostname is provided, then your own IP is looked up.

Usage

package main

import (
	"context"
	"github.com/Shivam010/go-freeGeoIP"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
	"time"
)

func main() {
	ctx := context.Background()

	// Using default client which comes with an in-memory cache implementation
	// with 24 Hour expiry and a http.Client timeout of 2 seconds and a default
	// `log.Logger`
	cli := freeGeoIP.DefaultClient()
	res := cli.GetGeoInfoFromString(ctx, "8.8.8.8")
	if err := res.Error; err != nil {
		log.Println(err)
		return
	}
	// first time retrieval and hence, not a cached output
	cli.Logger.Println(res.Cached) // false

	// Trying again
	res = cli.GetGeoInfoFromString(ctx, "8.8.8.8")
	if err := res.Error; err != nil {
		log.Println(err)
		return
	}
	cli.Logger.Println(res.Cached) // true

	// Using an empty client, which comes with default http client and no cache
	// and no logs
	cli = &freeGeoIP.Client{}
	res = cli.GetGeoInfo(ctx, freeGeoIP.IP{8, 8, 8, 8})
	if err := res.Error; err != nil {
		log.Println(err)
		return
	}

	// You can use the `ICache` interface and provide you any of you cache
	// implementation or can use the library's in-memory (thread safe) with
	// or without expiry.
	cache := freeGeoIP.NewCache(freeGeoIP.NoCacheExpiration,
		func(ctx context.Context, ip freeGeoIP.IP) time.Duration {
			// check ip pattern
			if value := ctx.Value("IP_Skip_Pattern"); value != nil {
				if pat, ok := value.(string); ok {
					if strings.Contains(ip.String(), pat) {
						// always skip caching such ip patterns
						return freeGeoIP.SkipCache
					}
				}
			}
			return freeGeoIP.NoCacheExpiration
		},
	)

	// And you can even provide your own combination of arguments in client
	// by providing a self cache implementation for `freeGeoIP.ICache` or the
	// the http.Client or the log.Logger
	// The below call to NewCache will create a non expiry cache implementation
	cache = freeGeoIP.NewCache(freeGeoIP.NoCacheExpiration, nil)
	cli = &freeGeoIP.Client{
		Cache:   cache,
		HttpCli: &http.Client{Timeout: time.Second},
		Logger:  log.New(ioutil.Discard, "", 0),
	}
	res = cli.GetGeoInfo(ctx, freeGeoIP.IP{8, 8, 8, 8})
	if err := res.Error; err != nil {
		log.Println(err)
		return
	}
}

Request for Contribution

Contributors are more than welcome and much appreciated. Please feel free to open a PR to improve anything you don't like, or would like to add.

Please make your changes in a specific branch and create a pull request into master! If you can, please make sure all the changes work properly and does not affect the existing functioning.

No PR is too small! Even the smallest effort is countable.

License

This project is licensed under the Apache License 2.0

go-freegeoip's People

Contributors

shivam010 avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.