Hello,
I wrote this to api@erepublik.com, but didn't receive any reply.. I would've liked this to be put on api.erepublik.com, together with the PHP version, but nevermind -- I'm posting it here
For those of you who use Python instead of PHP:
#!/usr/bin/python from requests import session from urllib import urlencode from datetime import datetime import hashlib import hmac import locale from pprint import pprint import cjson private = '' public = '' def api_call(resource, action, params = None): locale.setlocale(locale.LC_ALL, 'C') with session() as s: date = datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S') querystring = '' if params: querystring = urlencode(params) + ':' request_string = '%s:%s:%s%s' % (resource, action, querystring.lower(), date) print request_string auth = hmac.new(private, request_string, hashlib.sha256).hexdigest() headers = { 'Date': date, 'Auth': '%s/%s' % (public, auth), } print headers result = s.get('http://api.erepublik.com/%s/%s' % (resource, action), params=params, headers=headers) try: return cjson.decode(result.text) except: return result.text def main(): # result = api_call('countries', 'index') # result = api_call('citizen', 'profile', dict(citizenId = 1234)) # result = api_call('country', 'regions', dict(countryId = 10)) result = api_call('region', 'citizens', dict(regionId = 162, page = 12)) pprint(result) if __name__ == '__main__': main()
Setting the locale to C is needed for correct formatting of the date.
Hope it helps!