Skip to content

Commit

Permalink
refactor: reformat for easier readability
Browse files Browse the repository at this point in the history
  • Loading branch information
BenB196 committed Oct 12, 2024
1 parent 071705c commit a26dc2c
Showing 1 changed file with 52 additions and 52 deletions.
104 changes: 52 additions & 52 deletions ip_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,52 @@ import (
"strings"
)

//URI for the free IP-API
const FreeAPIURI = "http://ip-api.com/"
// FreeApiUri URI for the free IP-API
const FreeApiUri = "http://ip-api.com/"

//URI for the pro IP-API
const ProAPIURI = "https://pro.ip-api.com/"
// ProApiUri URI for the pro IP-API
const ProApiUri = "https://pro.ip-api.com/"

type Location struct {
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"`
Lat *float32 `json:"lat,omitempty"`
Lon *float32 `json:"lon,omitempty"`
Timezone string `json:"timezone,omitempty"`
Currency string `json:"currency,omitempty"`
ISP string `json:"isp,omitempty"`
Org 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"`
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"`
Lat *float32 `json:"lat,omitempty"`
Lon *float32 `json:"lon,omitempty"`
Timezone string `json:"timezone,omitempty"`
Currency string `json:"currency,omitempty"`
ISP string `json:"isp,omitempty"`
Org 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"`
Query string `json:"query,omitempty"`
}

type Query struct {
Queries []QueryIP `json:"queries"`
Fields string `json:"fields,omitempty"`
Lang string `json:"lang,omitempty"`
Queries []QueryIP `json:"queries"`
Fields string `json:"fields,omitempty"`
Lang string `json:"lang,omitempty"`
}

type QueryIP struct {
Query string `json:"query"`
Fields string `json:"fields,omitempty"`
Lang string `json:"lang,omitempty"`
Query string `json:"query"`
Fields string `json:"fields,omitempty"`
Lang string `json:"lang,omitempty"`
}

//Execute a single query (queries field should only contain 1 value
// SingleQuery Execute a single query (queries field should only contain 1 value
func SingleQuery(query Query, apiKey string, baseURL string, debugging bool) (*Location, error) {
//Make sure that there is only 1 query value
if len(query.Queries) != 1 {
Expand All @@ -67,17 +67,17 @@ func SingleQuery(query Query, apiKey string, baseURL string, debugging bool) (*L
}

//Build URI
uri := buildURI(query, "single",apiKey, baseURL)
uri := buildURI(query, "single", apiKey, baseURL)

//Execute query
req, err := http.NewRequest("GET",uri,nil)
req, err := http.NewRequest("GET", uri, nil)

if err != nil {
return nil, err
}

//Set request headers
req.Header.Set("Accept","application/json")
req.Header.Set("Accept", "application/json")

resp, err := http.DefaultClient.Do(req)

Expand Down Expand Up @@ -111,15 +111,15 @@ func SingleQuery(query Query, apiKey string, baseURL string, debugging bool) (*L
return &location, nil
}

//Execute a batch query (queries field should contain 1 or more values
// BatchQuery Execute a batch query (queries field should contain 1 or more values
func BatchQuery(query Query, apiKey string, baseURL string, debugging bool) ([]Location, error) {
//Make sure that there are 1 or more query values
if len(query.Queries) < 1 {
return nil, errors.New("error: no queries passed to batch query")
}

//Build URI
uri := buildURI(query,"batch",apiKey, baseURL)
uri := buildURI(query, "batch", apiKey, baseURL)

//Build queries list
queries, err := json.Marshal(query.Queries)
Expand All @@ -133,14 +133,14 @@ func BatchQuery(query Query, apiKey string, baseURL string, debugging bool) ([]L
}

//Execute Query
req, err := http.NewRequest("POST",uri,bytes.NewReader(queries))
req, err := http.NewRequest("POST", uri, bytes.NewReader(queries))

if err != nil {
return nil, err
}

//Set request headers
req.Header.Set("Content-Type","application/json")
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)

Expand Down Expand Up @@ -171,7 +171,7 @@ func BatchQuery(query Query, apiKey string, baseURL string, debugging bool) ([]L
return nil, err
}

return locations,nil
return locations, nil
}

func buildURI(query Query, queryType string, apiKey string, baseURL string) string {
Expand All @@ -182,9 +182,9 @@ func buildURI(query Query, queryType string, apiKey string, baseURL string) stri
} else {
switch apiKey {
case "":
baseURI = FreeAPIURI
baseURI = FreeApiUri
default:
baseURI = ProAPIURI
baseURI = ProApiUri
}
}

Expand Down Expand Up @@ -231,26 +231,26 @@ func buildURI(query Query, queryType string, apiKey string, baseURL string) stri
return baseURI
}

//Build fields string from slice
// Build fields string from slice
func buildFieldList(fields string) string {
return "fields=" + fields
}

//Build lang string from lang value
// Build lang string from lang value
func buildLangString(lang string) string {
return "lang=" + lang
}

var AllowedAPIFields = []string{"status","message","continent","continentCode","country","countryCode","region","regionName","city","district","zip","lat","lon","timezone","isp","org","as","asname","reverse","mobile","proxy","hosting","query"}
var AllowedAPIFields = []string{"status", "message", "continent", "continentCode", "country", "countryCode", "region", "regionName", "city", "district", "zip", "lat", "lon", "timezone", "isp", "org", "as", "asname", "reverse", "mobile", "proxy", "hosting", "query"}

var AllowedLangs = []string{"en","de","es","pt-BR","fr","ja","zh-CN","ru"}
var AllowedLanguages = []string{"en", "de", "es", "pt-BR", "fr", "ja", "zh-CN", "ru"}

/*
ValidateFields - validates the fields string to make sure it only has valid parameters
fields - string of comma separated values
*/
func ValidateFields(fields string) (string, error) {
fieldsSlice := strings.Split(fields,",")
fieldsSlice := strings.Split(fields, ",")

for _, field := range fieldsSlice {
if !contains(AllowedAPIFields, field) {
Expand All @@ -266,7 +266,7 @@ ValidateLang - validates the lang string to make sure it is a valid lang option
lang - string with lang value
*/
func ValidateLang(lang string) (string, error) {
if !contains(AllowedLangs,lang) {
if !contains(AllowedLanguages, lang) {
return "", errors.New("error: illegal lang value provided: " + lang)
}

Expand All @@ -288,4 +288,4 @@ func contains(slice []string, item string) bool {
}
}
return false
}
}

0 comments on commit a26dc2c

Please sign in to comment.