Beispiel #1
0
    def get_user(self):
        """Does a GET request to /user/.

        Endpoint for basic user information

        Returns:
            User: Response from the API.

        Raises:
            APIException: When an error occurs while fetching the data from
                the remote API. This exception includes the HTTP Response
                code, an error message, and the HTTP body that was received in
                the request.

        """
        # The base uri for api requests
        query_builder = Configuration.BASE_URI

        # Prepare query string for API call
        query_builder += "/user/"

        # Validate and preprocess url
        query_url = APIHelper.clean_url(query_builder)

        # Prepare headers
        headers = {
            "Authorization": "Bearer " + Configuration.o_auth_access_token,
            "user-agent": "WeGene SDK",
            "accept": "application/json",
        }

        # Prepare and invoke the API call request to fetch the response
        response = requests.get(query_url, headers=headers)

        # Error handling using HTTP status codes
        if response.status_code < 200 or response.status_code > 206:  # 200 = HTTP OK
            raise APIException("HTTP Response Not OK", response.status_code,
                               response.json())

        # Try to cast response to desired type
        if isinstance(response.json(), dict):
            # Response is already in a dictionary, return the object
            try:
                return User(**response.json())
            except TypeError:
                raise APIException("Invalid JSON returned",
                                   response.status_code, response.json())

        # If we got here then an error occured while trying to parse the response
        raise APIException("Invalid JSON returned", response.status_code,
                           response.json())
Beispiel #2
0
    def get_allele(self, profile_id, locations):
        """Does a POST request to /genotypes/{profile_id}.

        Allele data

        Args:
            locations (string): Allele location separated by space
            profile_id (string): Genetic Profile Id

        Returns:
            mixed: Response from the API.

        Raises:
            APIException: When an error occurs while fetching the data from
                the remote API. This exception includes the HTTP Response
                code, an error message, and the HTTP body that was received in
                the request.

        """
        # The base uri for api requests
        query_builder = Configuration.BASE_URI

        # Prepare query string for API call
        query_builder += "/genotypes/{profile_id}"

        # Process optional template parameters
        query_builder = APIHelper.append_url_with_template_parameters(
            query_builder, {"profile_id": profile_id})

        # Validate and preprocess url
        query_url = APIHelper.clean_url(query_builder)

        # Prepare headers
        headers = {
            "Authorization": "Bearer " + Configuration.o_auth_access_token,
            "user-agent": "WeGene SDK",
            "accept": "application/json",
        }

        # Prepare parameters
        parameters = {"locations": ' '.join(locations)}

        # Prepare and invoke the API call request to fetch the response
        response = requests.post(query_url, headers=headers, data=parameters)

        # Error handling using HTTP status codes
        if response.status_code < 200 or response.status_code > 206:  # 200 = HTTP OK
            raise APIException("HTTP Response Not OK", response.status_code,
                               response.json())

        return response.json()
Beispiel #3
0
    def get_drug(self, profile_id, report_id):
        """Does a POST request to /health/drug/{profile_id}.

        Drug response based on genetic information

        Args:
            profile_id (string): Genetic profile Id
            report_id (string): Report Id for the specific drug response to
                look

        Returns:
            Report: Response from the API.

        Raises:
            APIException: When an error occurs while fetching the data from
                the remote API. This exception includes the HTTP Response
                code, an error message, and the HTTP body that was received in
                the request.

        """
        # The base uri for api requests
        query_builder = Configuration.BASE_URI

        # Prepare query string for API call
        query_builder += "/health/drug/{profile_id}"

        # Process optional template parameters
        query_builder = APIHelper.append_url_with_template_parameters(
            query_builder, {"profile_id": profile_id})

        # Validate and preprocess url
        query_url = APIHelper.clean_url(query_builder)

        # Prepare headers
        headers = {
            "Authorization": "Bearer " + Configuration.o_auth_access_token,
            "user-agent": "WeGene SDK",
            "accept": "application/json",
        }

        # Prepare parameters
        parameters = {"report_id": report_id}

        # Prepare and invoke the API call request to fetch the response
        response = requests.post(query_url, headers=headers, data=parameters)

        # Error handling using HTTP status codes
        if response.status_code < 200 or response.status_code > 206:  # 200 = HTTP OK
            raise APIException("HTTP Response Not OK", response.status_code,
                               response.json())

        # Try to cast response to desired type
        if isinstance(response.json(), dict):
            # Response is already in a dictionary, return the object
            try:
                return Report(**response.json())
            except TypeError:
                raise APIException("Invalid JSON returned",
                                   response.status_code, response.json())

        # If we got here then an error occured while trying to parse the response
        raise APIException("Invalid JSON returned", response.status_code,
                           response.json())
Beispiel #4
0
    def get_advice(self,
                      profile_id,
                      sex,
                      age,
                      height,
                      weight,
                      health_target):
        """Does a POST request to /sport/advise/{profile_id}.

        Sports advice based on genetic and individual information

        Args:
            age (int): Age of the user
            health_target (string): Target for sport, only supporting slimming
                now
            height (int): Height of the user
            profile_id (string): Genetic profile Id
            sex (string): Sex of the user
            weight (int): Weight of the user

        Returns:
            SportAdvice: Response from the API.

        Raises:
            APIException: When an error occurs while fetching the data from
                the remote API. This exception includes the HTTP Response
                code, an error message, and the HTTP body that was received in
                the request.

        """
        # The base uri for api requests
        query_builder = Configuration.BASE_URI

        # Prepare query string for API call
        query_builder += "/sport/advise/{profile_id}"

        # Process optional template parameters
        query_builder = APIHelper.append_url_with_template_parameters(query_builder, {
            "profile_id": profile_id
        })

        # Validate and preprocess url
        query_url = APIHelper.clean_url(query_builder)

        # Prepare headers
        headers = {

            "Authorization": "Bearer "+ Configuration.o_auth_access_token,
            "user-agent": "APIMATIC 2.0",
            "accept": "application/json",

        }

        # Prepare parameters
        parameters = {
            "age": age,
            "health_target": health_target,
            "height": height,
            "sex": sex,
            "weight": weight
        }

        # Prepare and invoke the API call request to fetch the response
        response = unirest.post(query_url, headers=headers, params=parameters)

        # Error handling using HTTP status codes
        if response.code < 200 or response.code > 206:  # 200 = HTTP OK
            raise APIException("HTTP Response Not OK", response.code, response.body)

        # Try to cast response to desired type
        if isinstance(response.body, dict):
            # Response is already in a dictionary, return the object
            try:
                return SportAdvice(**response.body)
            except TypeError:
                raise APIException("Invalid JSON returned", response.code, response.body)

        # If we got here then an error occured while trying to parse the response
        raise APIException("Invalid JSON returned", response.code, response.body)