Skip to content

Instantly share code, notes, and snippets.

View cep21's full-sized avatar

Jack Lindamood cep21

View GitHub Profile
@cep21
cep21 / simulation.py
Created April 11, 2014 22:37
Odds of predicting a 49.5 % coin is worse than a 50% coin
import random
def simulate(a, b, iterations):
acount = bcount = 0
for i in xrange(iterations):
vala = random.uniform(0, a)
valb = random.uniform(0, b)
if vala > valb:
acount+=1
elif vala < valb:
bcount += 1
75a76
> num_setable = 0;
271c272
< int num_setable = 0;
---
> int num_setable;
@cep21
cep21 / client_test.go
Last active June 30, 2016 17:50
Example client library test
func ItemTest(t *testing.T) {
// Common setup. Abstract this out
// This allows each test to create its own handler by changing handler variable
handler := http.NotFound
hs := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
handler(rw, req)
}))
defer hs.Close()
// Notice I set the base URL of the client to the httptest server
c := Client{
@cep21
cep21 / client_integration_test.go
Last active June 30, 2016 22:11
Example integration test of a client
// +build integration
package smiteclient
// Create a file named info.json and put it at the root of your project. That file should have your
// devId and authKey. The file should be inside .gitignore and not checked into git. Then,
// run `go test -v --tags=integration .` to start integration tests using your auth key.
type devInfo struct {
AuthKey string
}
@cep21
cep21 / client.go
Created July 1, 2016 16:01
Example supporting default empty struct
const DefaultBaseURL = "http://api.smitegame.com/"
func (c *Client) urlBase() string {
if c.BaseURL == "" {
return DefaultBaseURL
}
return c.BaseURL
}
@cep21
cep21 / client.go
Last active July 7, 2016 23:37
Example draining http response
func (c *Client) doRequest(req *http.Request) {
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer func() {
maxCopySize := 2 << 10
io.CopyN(ioutil.Discard, resp.Body, maxCopySize)
resp.Close()
}()
@cep21
cep21 / client_trace.go
Created August 26, 2016 22:40
ClientTrace object
type ClientTrace struct {
// GetConn is called before a connection is created or
// retrieved from an idle pool. The hostPort is the
// "host:port" of the target or proxy. GetConn is called even
// if there's already an idle cached connection available.
GetConn func(hostPort string)
// GotConn is called after a successful connection is
// obtained. There is no hook for failure to obtain a
// connection; instead, use the error from
type ClientTrace struct {
// Called before a connection is established. If it returns a
// non nil object, that object is used instead for the Connection.
GetConn func(hostPort string) *net.Conn
}
type ClientTrace struct {
// Called before a connection is established. The returned context is used
// to finish processing the request.
GetConn func(ctx context.Context, hostPort string) context.Context
}
type Request struct {
Method string
URL *url.URL
// ...
// Trace callbacks are executed if they are set
Trace ClientTrace
}