Last active
April 3, 2016 22:36
-
-
Save kevinburke/420afcc1cb1116ac1c5a91af900fd702 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"net" | |
"net/http" | |
"sync" | |
) | |
type JsonIpResponse struct { | |
IP net.IP `json:"ip"` | |
} | |
type IpInfoResponse struct { | |
IP net.IP `json:"ip"` | |
} | |
func makeJsonipRequest() (JsonIpResponse, error) { | |
resp, err := http.Get("http://jsonip.com") | |
if err != nil { | |
return JsonIpResponse{}, err | |
} | |
var jip JsonIpResponse | |
defer resp.Body.Close() | |
err = json.NewDecoder(resp.Body).Decode(&jip) | |
return jip, err | |
} | |
func ipInfoRequest() (IpInfoResponse, error) { | |
req, err := http.NewRequest("GET", "http://ipinfo.io", nil) | |
if err != nil { | |
return IpInfoResponse{}, err | |
} | |
req.Header.Add("Accept", "application/json") | |
req.Header.Add("User-Agent", "HTTPie/1.0.0-dev") | |
//b, _ := httputil.DumpRequestOut(req, true) | |
//os.Stderr.Write(b) | |
resp, err := http.DefaultClient.Do(req) | |
//r := io.TeeReader(resp.Body, os.Stdout) | |
if err != nil { | |
return IpInfoResponse{}, err | |
} | |
var jip IpInfoResponse | |
defer resp.Body.Close() | |
err = json.NewDecoder(resp.Body).Decode(&jip) | |
return jip, err | |
} | |
func main() { | |
var jipr JsonIpResponse | |
var jerr error | |
var ipr IpInfoResponse | |
var ierr error | |
// These two functions can be anything you want to do in parallel; | |
// - make internal HTTP requests for billing info, account info, and fraud scores | |
// - read records from different database tables | |
// | |
// This was the simplest example I could demonstrate, that would run. | |
var wg sync.WaitGroup | |
go func() { | |
ipr, ierr = ipInfoRequest() | |
wg.Done() | |
}() | |
wg.Add(1) | |
go func() { | |
jipr, jerr = makeJsonipRequest() | |
wg.Done() | |
}() | |
wg.Add(1) | |
wg.Wait() | |
fmt.Println("jsonip.com ip result:", jipr) | |
fmt.Println("ipinfo.io ip result:", ipr) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var compareIps = function(userId, pickupId) { | |
return Promise.join( | |
getJsonIp(), | |
getIpInfoIp() | |
).spread(function(ipinfoIp, jsonIp) { | |
console.log(ipinfoIp); | |
console.log(jsonIp); | |
}).catch(function(err){ | |
console.error(err); | |
process.exit(1); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the implementation of getJsonIp and getIpInfoIp? I presume they return Promise objects? So an equivalent would be async Go funcs that return channels? If we're contriving it like that, then the code becomes a lot more similar:
Or could have somekind of container struct that knows about both fetching types and use a scanner pattern to check for errors.