How to use the stravalib.exc.Fault function in stravalib

To help you get started, we’ve selected a few stravalib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hozn / stravalib / stravalib / protocol / scrape.py View on Github external
soup = BeautifulSoup(r.text)
        
        authenticity_token = soup.find('input', attrs=dict(name="authenticity_token"))['value']
        
        params = {'_method': 'post',
                  'authenticity_token': authenticity_token}
        
        files = {'files[]': fileobj}
        r = self.rsession.post('http://app.strava.com/upload/files', data=params, files=files, cookies=self.cookies)
        r.raise_for_status()
        
        matches = re.search(r'var uploadingIds\s*=\s*\[([\d,]+)\];', r.text)
        if not matches:
            self.log.debug(r.text)
            raise exc.Fault("Unable to locate upload IDs in reponse (enable debug logging to see response html page)")
        
        upload_ids = [int(u_id.strip()) for u_id in matches.group(1).split(',')] 
        
        return upload_ids
github hozn / stravalib / stravalib / protocol / __init__.py View on Github external
def _handle_protocol_error(self, response):
        """
        Parses the JSON response from the server, raising a :class:`stravalib.exc.Fault` if the
        server returned an error.
        
        :param response: The response JSON
        :raises Fault: If the response contains an error. 
        """
        if 'error' in response:
            raise exc.Fault(response['error'])
        return response
github hozn / stravalib / stravalib / protocol.py View on Github external
exc_class = None
        if response.status_code == 404:
            msg = '%s: %s' % (response.reason, error_str)
            exc_class = exc.ObjectNotFound
        elif response.status_code == 401:
            msg = '%s: %s' % (response.reason, error_str)
            exc_class = exc.AccessUnauthorized
        elif 400 <= response.status_code < 500:
            msg = '%s Client Error: %s [%s]' % (response.status_code, response.reason, error_str)
            exc_class = exc.Fault
        elif 500 <= response.status_code < 600:
            msg = '%s Server Error: %s [%s]' % (response.status_code, response.reason, error_str)
            exc_class = exc.Fault
        elif error_str:
            msg = error_str
            exc_class = exc.Fault

        if exc_class is not None:
            raise exc_class(msg, response=response)

        return response
github hozn / stravalib / stravalib / exc.py View on Github external
"""


class UnboundEntity(RuntimeError):
    """
    Exception used to indicate that a model Entity is not bound to client instances.
    """


class Fault(requests.exceptions.HTTPError):
    """
    Container for exceptions raised by the remote server.
    """


class ObjectNotFound(Fault):
    """
    When we get a 404 back from an API call.
    """


class AccessUnauthorized(Fault):
    """
    When we get a 401 back from an API call.
    """


class RateLimitExceeded(RuntimeError):
    """
    Exception raised when the client rate limit has been exceeded.

    http://strava.github.io/api/#access
github hozn / stravalib / stravalib / protocol / scrape.py View on Github external
"distance":"15.5","long_unit":"miles","short_unit":"mi","moving_time":"00:58:27","moving_time_raw":3507,"elapsed_time":"01:01:40","trainer":false,"commute":false,"elevation_gain":"712",
            "description":null,"activity_url":"http://app.strava.com/activities/46504896",
            "activity_url_for_twitter":"http://app.strava.com/activities/46504896?ref=1MT1yaWRlX3NoYXJlOzI9dHdpdHRlcjs0PTE4OTQ0MTE%253D",
            "twitter_msg":"went for a 15.5 mile ride.","static_map_url":"http://maps.google.com/maps/api/staticmap?maptype=terrain\u0026size=220x130\u0026sensor=false\u0026path=color:0xFF0000BF|weight:2|38.8828,-77.1436|38.8823,-77.1447|38.884,-77.1489|38.884,-77.1496|38.8833,-77.1501|38.8845,-77.1538|38.8829,-77.156|38.8824,-77.1591|38.8858,-77.1596|38.8869,-77.1606|38.89,-77.1733|38.8917,-77.1843|38.8919,-77.1887|38.8911,-77.2013|38.8898,-77.2064|38.8903,-77.2094|38.8914,-77.2114|38.8904,-77.2123|38.8896,-77.2144|38.8963,-77.2398|38.8971,-77.2419|38.9002,-77.2462|38.9009,-77.2478|38.9014,-77.2514|38.9009,-77.2575|38.9009,-77.2596|38.9013,-77.2611|38.9045,-77.2673|38.9154,-77.2797|38.9181,-77.2837|38.9202,-77.2854|38.9249,-77.2877|38.9273,-77.2904|38.9316,-77.2997|38.9328,-77.3055|38.9336,-77.3075|38.9369,-77.3134|38.9409,-77.3183|38.9419,-77.3215|38.9431,-77.3239|38.9471,-77.3276|38.9488,-77.3303|38.9535,-77.3463|38.9545,-77.3493|38.9567,-77.3536|38.9571,-77.3569|38.9564,-77.3611|38.9573,-77.357|38.9571,-77.3552|38.9486,-77.3578|38.944,-77.3609|38.9445,-77.3615|38.9441,-77.3618"}},
                  
        :param upload_id: A single upload ID to check.
        :type upload_id: int
        :return: Progress dict for specified file.
        :rtype: dict
        """
        r = self.rsession.get('http://app.strava.com/upload/progress.json',
                              params={'ids[]': upload_id},
                              cookies=self.cookies)
        progress = r.json()
        if not len(progress):
            raise exc.Fault("No matches for upload id: {0}".format(upload_id))
        else:
            for p in progress:
                if p['id'] == upload_id:
                    return p
            else:
                self.log.debug("upload status reponse: {0!r}".format(progress))
                raise exc.Fault("Statuses returned, but no matches for upload id: {0}".format(upload_id))
github hozn / stravalib / stravalib / exc.py View on Github external
"""


class Fault(requests.exceptions.HTTPError):
    """
    Container for exceptions raised by the remote server.
    """


class ObjectNotFound(Fault):
    """
    When we get a 404 back from an API call.
    """


class AccessUnauthorized(Fault):
    """
    When we get a 401 back from an API call.
    """


class RateLimitExceeded(RuntimeError):
    """
    Exception raised when the client rate limit has been exceeded.

    http://strava.github.io/api/#access
    """
    def __init__(self, msg, timeout=None, limit=None):
        super(RateLimitExceeded, self).__init__()
        self.limit = limit
        self.timeout = timeout