Skip to content

Golang module to simplify access to the iRacing /data API

License

Notifications You must be signed in to change notification settings

popmonkey/irdata

Repository files navigation

irdata

Golang module to simplify access to the iRacing /data API from go

Setup

go get github.com/popmonkey/irdata
api := irdata.Open(context.Background)

Authentication

You can use the provided utility function to request creds from the terminal:

var credsProvider irdata.CredsFromTerminal

api.AuthWithProvideCreds(credsProvider.GetCreds)

You can specify the username, password yourself:

var myCreds struct{}

func (myCreds) GetCreds() {
    return []byte("prost"), []byte("senna")
}

api.AuthWithProvidedCreds(myCreds)

You can also store your credentials in a file encrypted using a keyfile:

var credsProvider irdata.CredsFromTerminal

api.AuthAndSaveProvidedCredsToFile(keyFn, credsFn, credsProvider)

After you have a creds file you can load these into your session like so:

api.AuthWithCredsFromFile(keyFn, credsFn)

Creating and protecting the keyfile

For the key file, you need to create a random string of 16, 24, or 32 bytes and base64 encode it into a file. The file must be set to read only by user (0400) and it is recommended this lives someplace safe.

Example key file creation in Linux or OS X:

openssl rand -base64 32 > ~/my.key && chmod 0400 ~/my.key

Warning

Don't check your keys into git ;)

Accessing the /data API

Once authenticated, you can query the API by URI, for example:

data, err := api.Get("/data/member/info")

If successful, this returns a []byte array containing the JSON response. See the profile example for some json handling logic.

The API is lightly documented via the /data API itself. Check out the latest version and track changes to it.

Using the cache

The iRacing /data API imposes a rate limit which can become problematic especially when running your program over and over such as during development.

irdata therefore provides a caching layer which you can use to avoid making calls to the actual iRacing API.

api.EnableCache(".cache")

data, err := api.GetWithCache("/data/member/info", time.Duration(15)*time.Minute)

Subsequent calls to the same URI (with same parameters) over the next 15 minutes will return data from the local cache before calling the iRacing /data API again.

Chunked responses

Some iRacing data APIs returns data in chunks (e.g. /data/results/search_series). When irdata detects this it will fetch each chunk and then merge the results into an object array. This object array can be found in the new value _chunk_data which will be present where the chunk_info block was found.

Debugging

You can turn on verbose logging in order to debug your sessions. This will use the logrus module to write to stderr.

api.EnableDebug()

Development

git clone [email protected]:popmonkey/irdata.git

Note

Keyfiles must have permissions set to 0400. The example and test keys that are checked into this repository need to be adjust after cloning/pulling

chmod 0400 example/example.key testdata/test.key

Run tests:

go test

These tests run without actually reaching out to the API. To run the complete gamut of tests you need to specify an existing key and creds file (such as those created in the examples) by setting environment variables IRDATA_TEST_KEY to point to the keyfile and IRDATA_TEST_CREDS to point to the creds file encrypted by the key:

IRDATA_TEST_KEY=/path/to/key IRDATA_TEST_CREDS=/path/to/creds go test

Run examples:

pushd examples/profile
go run profile.go
popd