Examples for using the API

Before being able to use these examples, be sure to log in and register an app.

Creating a team using Python urllib2

This example is provided by Chris Schaffner from Amsterdam. Thanks, Chris!
  1. import simplejson
  2. import urllib2
  3. # Create a new app and copy the credentials it creates
  4. CLIENT_ID = # This is given to you when you create/register your app
  5. CLIENT_SECRET_KEY = # This is also given to you create/register your app
  6. BASE_URL = "https://www.leaguevine.com"
  7. BASE_API_URL = "https://api.leaguevine.com"
  8. # Make a request for an access_token
  9. access_token_url = ('{0}/oauth2/token/?client_id={1}&'
  10. 'client_secret={2}&grant_type=client_credentials&'
  11. 'scope=universal'.format(BASE_URL,
  12. CLIENT_ID,
  13. CLIENT_SECRET_KEY))
  14. response_data = urllib2.urlopen(access_token_url)
  15. # parse string into Python dictionary
  16. response_dict = simplejson.loads(response_data.read())
  17. response_data.close()
  18. access_token = response_dict.get('access_token')
  19. # Create a JSON object for the new team you want to create
  20. team_data_dict = {"name": "Test Team",
  21. "season_id": 5,
  22. "info": "Test info."}
  23. team_data = simplejson.dumps(team_data_dict)
  24. # Make a request to create a new team
  25. request = urllib2.Request(url='{0}/v1/teams/'.format(BASE_API_URL),
  26. data=team_data)
  27. request.add_header('Content-Type', 'application/json')
  28. request.add_header('Accept', 'application/json')
  29. request.add_header('Authorization', 'bearer {0}'.format(access_token))
  30. response_data = urllib2.urlopen(request)
  31. response_dict = simplejson.loads(response_data.read())

Debugging your urllib2 requests

If your requests are causing our server to send you errors, you will want to be able to read what those errors are. Here's how to do it using urllib2, working off the example above.
  1. request.data = "{'name': 'Test Team', 'info': 'Some invalid JSON'}"
  2. try:
  3. response_data = urllib2.urlopen(request)
  4. except urllib2.HTTPError, e:
  5. error_response = e.read()
  6. # error_response should contain something like:
  7. # '{"error_message": "The data passed in is not properly formatted JSON."}'

Removing Tournament Teams in Python using python-requests

This example is provided by Chris Schaffner from Amsterdam and uses a library called python-requests.
  1. import logging
  2. import requests # To install this library: pip install requests
  3. import simplejson # To install this library: pip install simplejson
  4. import sys
  5. # Create a new app and copy the credentials it creates into these variables:
  6. CLIENT_ID = 'a18d62e40f4d269996b01f7cf462a9'
  7. CLIENT_PWD = '93dbb28011a5224303074b3deebaf6'
  8. BASE_URL = "https://www.leaguevine.com"
  9. BASE_API_URL = "https://api.leaguevine.com"
  10. # ID of the tournament we want to delete games from
  11. tournament_id = 0
  12. # Get an instance of a logger
  13. logger = logging.getLogger('leaguevine.addons')
  14. # Make a request for an access_token
  15. r=requests.get(('{0}/oauth2/token/?client_id={1}&'
  16. 'client_secret={2}&grant_type=client_credentials&'
  17. 'scope=universal'.format(BASE_URL, CLIENT_ID, CLIENT_PWD)))
  18. # parse string into Python dictionary
  19. r_dict = simplejson.loads(r.content)
  20. access_token = r_dict.get('access_token')
  21. logger.info(access_token)
  22. # Save headers for this session
  23. my_headers={'Content-Type': 'application/json',
  24. 'Accept': 'application/json',
  25. 'Authorization': 'bearer {0}'.format(access_token)}
  26. my_config={'verbose': sys.stderr}
  27. # Retrieve all teams of a particular tournament
  28. url='{0}/v1/tournament_teams/?tournament_ids=[{1}]'.format(BASE_API_URL,tournament_id)
  29. next=True
  30. while next:
  31. # We do not use the next-url, but the original one because
  32. # we have removed some teams in the meantime
  33. response = requests.get(url=url,headers=my_headers,config=my_config)
  34. response_dict = simplejson.loads(response.content)
  35. logger.info(response_dict)
  36. for team in response_dict.get('objects'):
  37. # Remove this team from tournament.
  38. # Only teams created by the user who created the app will be removed
  39. remove_url='{0}/v1/tournament_teams/{1}/{2}/'.format(BASE_API_URL,tournament_id, team.get('team_id'))
  40. response = requests.delete(url=remove_url,headers=my_headers,config=my_config)
  41. if response.status_code == 204:
  42. logger.info('removed team with id {0}'.format(team.get('team_id')))
  43. else:
  44. response.raise_for_status()
  45. # Check if there are more teams
  46. next=response_dict.get('meta').get('next')

Fetching leagues in Javascript using jQuery

  1. <script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
  2. <script type="text/javascript">
  3. $(function() {
  4. $.ajax({
  5. url: "https://api.leaguevine.com/v1/leagues/?" +
  6. "organization_id=2" +
  7. "&sport=ultimate" +
  8. "&access_token=ACCESS_TOKEN",
  9. dataType: "json",
  10. contentType: "application/json",
  11. beforeSend: function(jqXHR, settings) {
  12. settings.accepts['json'] = "application/json";
  13. },
  14. success: function(data){
  15. for (i=0; i < data.objects.length; i++) {
  16. var obj = data.objects[i];
  17. // Do something with the objects
  18. }
  19. },
  20. });
  21. });
  22. </script>