Commit df45f0ec authored by harry's avatar harry

init

parents
Pipeline #14936 failed with stages
package gis
// GetGeoInfoInput get geo info input struct
type GetGeoInfoInput struct {
APIKey string `json:"api_key,omitempty"`
IP string `json:"ip,omitempty"`
IPs []string `json:"ips,omitempty"`
}
package gis
import (
"encoding/json"
"io/ioutil"
"net/http"
)
// SendV1GeoInfoJSONRequest send v1 geo info json request
func (s *GIS) SendV1GeoInfoJSONRequest(input *GetGeoInfoInput) (json.RawMessage, error) {
// New url
url := s.Host.String() + "/v1/geoinfo.json?ip=" + input.IP
// New request
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "application/json")
// Set api key
if len(input.APIKey) > 0 {
req.Header.Set("X-Api-Key", input.APIKey)
}
// Send request
resp, err := s.Client.Do(req)
if err != nil {
return nil, err
}
respBody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
return respBody, nil
}
// SendV1GeoInfoRequest send v1 geo info request
func (s *GIS) SendV1GeoInfoRequest(input *GetGeoInfoInput) (map[string]*GeoInfo, error) {
// Send json request
respBody, err := s.SendV1GeoInfoJSONRequest(input)
if err != nil {
return nil, err
}
// Parse body
var geoInfos map[string]*GeoInfo
if err := jsonex.Unmarshal(respBody, &geoInfos); err != nil {
return nil, err
}
return geoInfos, nil
}
// SendV2GeoInfoJSONRequest send v2 geo info json request
func (s *GIS) SendV2GeoInfoJSONRequest(input *GetGeoInfoInput) (json.RawMessage, error) {
// New url
url := s.Host.String() + "/v2/geoinfo.json?ip=" + input.IP
// New request
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "application/json")
// Set api key
if len(input.APIKey) > 0 {
req.Header.Set("X-Api-Key", input.APIKey)
}
// Send request
resp, err := s.Client.Do(req)
if err != nil {
return nil, err
}
respBody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
return respBody, nil
}
// SendV2GeoInfoRequest send v2 geo info request
func (s *GIS) SendV2GeoInfoRequest(input *GetGeoInfoInput) (*GeoInfo, error) {
// Send json request
respBody, err := s.SendV2GeoInfoJSONRequest(input)
if err != nil {
return nil, err
}
// Parse body
geoInfo := new(GeoInfo)
if err := jsonex.Unmarshal(respBody, geoInfo); err != nil {
return nil, err
}
return geoInfo, nil
}
package gis
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
// SendV1GeoInfosJSONRequest send v1 geo infos json request
func (s *GIS) SendV1GeoInfosJSONRequest(input *GetGeoInfoInput) (json.RawMessage, error) {
// New url
url := s.Host.String() + "/v1/geoinfos.json"
// New request body
data, err := jsonex.Marshal(input.IPs)
if err != nil {
return nil, err
}
// New request
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
// Set api key
if len(input.APIKey) > 0 {
req.Header.Set("X-Api-Key", input.APIKey)
}
// Send request
resp, err := s.Client.Do(req)
if err != nil {
return nil, err
}
respBody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
return respBody, nil
}
// SendV1GeoInfosRequest send v1 geo infos request
func (s *GIS) SendV1GeoInfosRequest(input *GetGeoInfoInput) (map[string]*GeoInfo, error) {
// Send json request
respBody, err := s.SendV1GeoInfosJSONRequest(input)
if err != nil {
return nil, err
}
// Parse body
var geoInfos map[string]*GeoInfo
if err := jsonex.Unmarshal(respBody, &geoInfos); err != nil {
return nil, err
}
return geoInfos, nil
}
// SendV2GeoInfosJSONRequest send v2 geo infos json request
func (s *GIS) SendV2GeoInfosJSONRequest(input *GetGeoInfoInput) (json.RawMessage, error) {
// New url
url := s.Host.String() + "/v2/geoinfos.json"
// New request body
data, err := jsonex.Marshal(IPsResource{input.IPs})
if err != nil {
return nil, err
}
// New request
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
// Set api key
if len(input.APIKey) > 0 {
req.Header.Set("X-Api-Key", input.APIKey)
}
// Send request
resp, err := s.Client.Do(req)
if err != nil {
return nil, err
}
respBody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
return respBody, nil
}
// SendV2GeoInfosRequest send v2 geo infos request
func (s *GIS) SendV2GeoInfosRequest(input *GetGeoInfoInput) (*GeoInfosResource, error) {
// Send json request
respBody, err := s.SendV2GeoInfosJSONRequest(input)
if err != nil {
return nil, err
}
// Parse body
geoInfos := new(GeoInfosResource)
if err := jsonex.Unmarshal(respBody, geoInfos); err != nil {
return nil, err
}
return geoInfos, nil
}
package gis
// Client gis client
var Client *GIS
// InitClient init gis client
func InitClient() {
Client = New()
}
package gis
import "os"
var endpoint = os.Getenv("GIS_ENDPOINT")
var apiKey = os.Getenv("GIS_API_KEY")
// SetEndpoint set endpoint
func SetEndpoint(s string) {
endpoint = s
}
// SetAPIKey set api key
func SetAPIKey(s string) {
apiKey = s
}
package gis
// GeoInfo geo info struct
type GeoInfo struct {
IP float64 `json:"ip,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
ContinentName string `json:"continent_name,omitempty"`
ContinentCode string `json:"continent_code,omitempty"`
CountryName string `json:"country_name,omitempty"`
CountryIsoCode string `json:"country_iso_code,omitempty"`
CityName string `json:"city_name,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
Coordinates string `json:"coordinates,omitempty"`
}
// GeoInfosResource geo infos resource struct
type GeoInfosResource struct {
GeoInfos []*GeoInfo `json:"geo_infos,omitempty"`
}
// IPsResource ips resource struct
type IPsResource struct {
IPs []string `json:"ips,omitempty"`
}
package gis
import "github.com/json-iterator/go"
var jsonex = jsoniter.ConfigCompatibleWithStandardLibrary
package gis
import (
"net/http"
"net/url"
"time"
)
// GIS service struct
type GIS struct {
Client *http.Client
Host *url.URL
}
// New get Service instance
func New() *GIS {
svc := &GIS{
Client: &http.Client{
Timeout: time.Duration(5 * time.Second),
Transport: &Transport{},
},
Host: &url.URL{
Scheme: "http",
Host: endpoint,
},
}
return svc
}
// Transport token transport
type Transport struct{}
// RoundTrip http Transport RoundTrip interface
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
if len(apiKey) > 0 {
req.Header.Set("X-Api-Key", apiKey)
}
return http.DefaultTransport.RoundTrip(req)
}
This diff is collapsed.
// Code generated by github.com/chouandy/goex/awsex/gen-api/main.go. DO NOT EDIT.
package gisapi
import (
"errors"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/chouandy/goex/awsex/service/apigatewayex"
"github.com/chouandy/goex/awsex/service/sfnex"
"github.com/chouandy/goex/httpex"
)
// Client gisapi client
var Client *GISAPI
// InitClient init gisapi client
func InitClient() error {
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
return err
}
cfg.Region = os.Getenv("REGION")
Client = New(cfg)
return nil
}
// InitClientMiddleware init gisapi client middleware
func InitClientMiddleware(ctx *apigatewayex.Context) error {
if Client == nil {
fmt.Print("[Middleware] Init GISAPI Client...")
if err := InitClient(); err != nil {
fmt.Println(err)
return httpex.NewError(500, "", "Failed to init gisapi client")
}
fmt.Println("done")
}
return nil
}
// InitClientTaskMiddleware init gisapi client task middleware
func InitClientTaskMiddleware(ctx *sfnex.Context) error {
if Client == nil {
fmt.Print("[Middleware] Init GISAPI Client...")
if err := InitClient(); err != nil {
fmt.Println(err)
return errors.New("Failed to init gisapi client")
}
fmt.Println("done")
}
return nil
}
// Code generated by github.com/chouandy/goex/awsex/gen-api/main.go. DO NOT EDIT.
package gisapi
import (
"os"
)
var scheme = os.Getenv("GISAPI_SCHEME")
var endpoint = os.Getenv("GISAPI_ENDPOINT")
var apiKey = os.Getenv("GISAPI_API_KEY")
// SetScheme set scheme
func SetScheme(s string) {
scheme = s
}
// SetEndpoint set endpoint
func SetEndpoint(s string) {
endpoint = s
}
// SetAPIKey set api key
func SetAPIKey(s string) {
apiKey = s
}
// Code generated by github.com/chouandy/goex/awsex/gen-api/main.go. DO NOT EDIT.
package gisapi
import (
"github.com/aws/aws-sdk-go-v2/aws"
)
func init() {
initClient = func(s *GISAPI) {
// Set default request headers
s.Handlers.Build.PushBack(func(r *aws.Request) {
r.HTTPRequest.Header.Add("Accept", "application/json")
r.HTTPRequest.Header.Add("X-Api-Key", apiKey)
})
// Set url
var url string
if len(scheme) > 0 {
url = scheme + "://" + endpoint
} else {
url = "https://" + endpoint
}
s.Client.Config.EndpointResolver = aws.ResolveWithEndpointURL(url)
}
}
// Code generated by github.com/chouandy/goex/awsex/gen-api/main.go. DO NOT EDIT.
package gisapi
import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/aws/aws-sdk-go-v2/private/protocol/restjson"
)
// GISAPI provides the API operation methods for making requests to
// GISAPI. See this package's package overview docs
// for details on the service.
//
// GISAPI methods are safe to use concurrently. It is not safe to
// modify mutate any of the struct's properties though.
type GISAPI struct {
*aws.Client
}
// Used for custom client initialization logic
var initClient func(*GISAPI)
// Used for custom request initialization logic
var initRequest func(*GISAPI, *aws.Request)
// Service information constants
const (
ServiceName = "execute-api" // Service endpoint prefix API calls made to.
EndpointsID = ServiceName // Service ID for Regions and Endpoints metadata.
)
// New creates a new instance of the GISAPI client with a config.
//
// Example:
// // Create a GISAPI client from just a config.
// svc := gisapi.New(myConfig)
func New(config aws.Config) *GISAPI {
var signingName string
signingRegion := config.Region
svc := &GISAPI{
Client: aws.NewClient(
config,
aws.Metadata{
ServiceName: ServiceName,
SigningName: signingName,
SigningRegion: signingRegion,
APIVersion: "2015-07-09",
},
),
}
// Handlers
svc.Handlers.Sign.PushBackNamed(v4.SignRequestHandler)
svc.Handlers.Build.PushBackNamed(restjson.BuildHandler)
svc.Handlers.Unmarshal.PushBackNamed(restjson.UnmarshalHandler)
svc.Handlers.UnmarshalMeta.PushBackNamed(restjson.UnmarshalMetaHandler)
svc.Handlers.UnmarshalError.PushBackNamed(restjson.UnmarshalErrorHandler)
// Run custom client initialization if present
if initClient != nil {
initClient(svc)
}
return svc
}
// newRequest creates a new request for a GISAPI operation and runs any
// custom request initialization.
func (c *GISAPI) newRequest(op *aws.Operation, params, data interface{}) *aws.Request {
req := c.NewRequest(op, params, data)
// Run custom request initialization if present
if initRequest != nil {
initRequest(c, req)
}
return req
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment