Skip to content
This repository has been archived by the owner on Feb 27, 2019. It is now read-only.

Added method to return all matches for a single account #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gregaz
Copy link

@gregaz gregaz commented May 6, 2016

Let me know if this looks like it would be a useful addition

@joshuaduffy
Copy link
Owner

Hey,

This does seem like a useful piece of functionality and thank you for taking the time to do this. I am not sure that we should be relaying requests when we've already been throttled with a 503.

Thoughts? @evaldobratti

@gregaz
Copy link
Author

gregaz commented May 25, 2016

The only info I could find from Valve's forum on limits to their API is below:
Are there limits on how many API calls?
Not presently, but you may get a 503 Error if the matchmaking server is busy or you exceed limits. Please wait 30 seconds and try again. A good rule of thumb is limit your requests to about one per second.
source: http://dev.dota2.com/showthread.php?t=47115

Currently upon receiving a 503, the code will wait a random time between 5 and 60 seconds, then retry the request (this can only happen up to 5 times before it will throw an error). I could change the minimum wait time to be 30 seconds as suggested by valve if you think that would be better. I've also got some 1 second sleeps in there to make sure we aren't making more than one request per second. Open to any other ideas as well.

@davidfischer
Copy link
Contributor

I realize this PR is a bit stale but retries and sleeps can be handled directly in requests without any additional libraries.

import os

import dota2api
import requests
from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter


# Retry requests automatically with a telescoping backoff factor
retries = Retry(total=5, backoff_factor=15, status_forcelist=[500, 502, 503, 504])
s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=retries))
s.mount('https://', HTTPAdapter(max_retries=retries))


# Setup a Dota2 api executor with a 5s timeout and up to 5 retries
# You could do other things here like sleep for 1s after making an API request
#  like you're supposed to...
def retry_request(url, *args, **kwargs):
    kwargs['timeout'] = 5
    return s.get(url, *args, **kwargs)


# Initialize the API with retries
API = dota2api.Initialise(os.environ.get('D2_API_KEY'), executor=retry_request)

print API.get_league_listing()

@Viech
Copy link

Viech commented Dec 18, 2017

I made my script to exponentially increase sleep time up to some maximum whenever a request fails, and decrease it linearly by a small amount whenever a request succeeds. It turns out that currently I am sending a request every 5.1 seconds on average this way, so a fixed sleep time between 5 and 10 seconds should be safe.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants