Пример #1
0
    def options(self):
        """
        Retrieves documentation of the REST resource representation.

        If the status code is 200 (OK), returns the documentation.  Otherwise,
        returns an error.

        The response is an object with the following attributes:

        +------------+------------------------+
        | Attribute  | Description            |
        +============+========================+
        | headers    | Response headers       |
        +------------+------------------------+
        | data       | Resource documentation |
        +------------+------------------------+

        Link resources are not included in the documentation.

        :returns: Resource documentation data.
        :rtype: ``EasyDict`` object.
        """
        uri = "%s%s" % (self.base_uri, self.endpoint)

        resp = requests.options(uri, params=self.auth)

        if resp.status_code == 200:
            return Response(self.base_uri, self.auth, resp)
        else:
            raise SoccermetricsRestException(resp.status_code, resp.url)
Пример #2
0
    def head(self):
        """
        Retrieves header data of the REST resource.

        The response is an object with the following attribute:

        +------------+-----------------------+
        | Attribute  | Description           |
        +============+=======================+
        | headers    | Response headers      |
        +------------+-----------------------+

        If the request is successful, the client returns an ``EasyDict`` object that
        uses dot notation for the response contents.

        If the request is unsuccessful (HTTP code 4xx or 5xx), the client raises a
        :exc:`~soccermetrics.SoccermetricsRestException` that includes the HTTP status
        code and the request URI.

        :returns: Header data.
        :rtype: ``EasyDict`` object.
        """
        uri = "%s%s" % (self.base_uri, self.endpoint)

        resp = requests.head(uri, params=self.auth)

        if resp.status_code < 400:
            return EasyDict(dict(headers=resp.headers))
        else:
            raise SoccermetricsRestException(resp.status_code, resp.url)
Пример #3
0
    def test_exception_with_msg(self):
        local = SoccermetricsRestException(404,
                                           "/path/to/resource",
                                           msg="Invalid resource request.")

        self.assertEqual(
            str(local),
            "HTTP ERROR 404: Invalid resource request. \n /path/to/resource")
Пример #4
0
    def get(self, uid=None, **kwargs):
        """
        Retrieves a representation of the REST resource.

        If the request is successful, returns an ``EasyDict`` object that
        uses dot notation for the response contents.

        A request may be unsuccessful for two reasons:

            * The API returns an error (HTTP code not 200).  In this situation,
              the client raises a :exc:`~soccermetrics.SoccermetricsRestException` with
              the HTTP code, the request URI, and a detailed error message.
            * The ``requests`` module raises an exception.  In this case, the client
              raises a :exc:`~soccermetrics.SoccermetricsRestException` with HTTP code
              500, the request URI and a detailed error message from the module.

        :param uid: Unique ID of API resource representation.
        :type uid: integer
        :param kwargs: Collection of query parameters.
        :type kwargs: dict
        :returns: Resource representation.
        :rtype: ``EasyDict`` object.
        """
        uri = "%s%s/%s" % (self.base_uri, self.endpoint, str(uid)) if uid else \
            "%s%s" % (self.base_uri, self.endpoint)

        full_param_dict = dict(kwargs, **self.auth)

        try:
            resp = requests.get(uri, params=full_param_dict)
            if resp.status_code == 200:
                return Response(self.base_uri, self.auth, resp)
            else:
                data = resp.json()
                raise SoccermetricsRestException(resp.status_code, data['uri'],
                                                 data['message'])
        except requests.exceptions.RequestException as e:
            raise SoccermetricsRestException(500, uri, msg=e)
Пример #5
0
    def get(self, uid=None, **kwargs):
        """
        Retrieves a representation of REST resource.

        The response is an object with the following attributes:

        +------------+-----------------------+
        | Attribute  | Description           |
        +============+=======================+
        | headers    | Response headers      |
        +------------+-----------------------+
        | meta       | Response meta-data    |
        +------------+-----------------------+
        | data       | Response data         |
        +------------+-----------------------+

        :param uid: Unique ID of API resource representation.
        :type uid: integer
        :param kwargs: Collection of query parameters.
        :type kwargs: dict
        :returns: Resource representation.
        :rtype: ``EasyDict`` object.
        """
        uri = "%s%s/%d" % (self.base_uri, self.endpoint, uid) if uid else \
            "%s%s" % (self.base_uri, self.endpoint)

        full_param_dict = dict(kwargs, **self.auth)

        try:
            resp = requests.get(uri,params=full_param_dict)
            if resp.status_code == 200:
                return Response(self.base_uri, self.auth, resp)
            else:
                data = resp.json()
                raise SoccermetricsRestException(resp.status_code,data['uri'],data['message'])
        except requests.exceptions.RequestException as e:
            raise SoccermetricsRestException(500, uri, msg=e)
Пример #6
0
    def head(self):
        """
        Retrieves header data of REST resource.

        The response is an object with the following attribute:

        +------------+-----------------------+
        | Attribute  | Description           |
        +============+=======================+
        | headers    | Response headers      |
        +------------+-----------------------+

        :returns: Header data.
        :rtype: ``EasyDict`` object.
        """
        uri = "%s%s" % (self.base_uri, self.endpoint)

        resp = requests.head(uri, params=self.auth)

        if resp.status_code < 400:
            return EasyDict(dict(headers=resp.headers))
        else:
            raise SoccermetricsRestException(resp.status_code, resp.url)
Пример #7
0
 def setUp(self):
     self.exc = SoccermetricsRestException(404, "/path/to/resource")