Example #1
0
    def name_gender_api(self, data=None):
        """ Sending POST request to the name-gender api"""
        """
        :param data: Pass your desired names in the dictionary format where each string has some unique key. (ex. {0: "ron"})
        :type data: dictionary
        :return: server response in JSON object 
        """

        payload = {}

        if self.api_key is None:
            raise ValueError("Please provide your private API Key")

        # Check if valid data dictionary
        if data is not None:
            if is_valid_dict(data):
                payload["data"] = data
            else:
                raise TypeError("Data should be of type dictionary")
        else:
            raise ValueError("Please provide data, data can not be empty")

        # Make a POST request to constants.NAME_GENDER_URL
        response = self.request_method.post(constants.NAME_GENDER_URL,
                                            auth=self.header,
                                            timeout=300,
                                            data=json.dumps(payload, indent=4))

        # Check the status code of the response if not equal to 200, then raise exception
        if response.status_code != 200:
            raise BytesviewException(response.json())

        # Return the response json
        return response.json()
Example #2
0
    def sentiment_api(self, data=None, lang="en"):
        """ Sending POST request to the sentiment api"""
        """
        :param data: pass your desired strings in the dictionary format where each string has some unique key. (ex. {0: "this is good"})
        :type data: dictionary
        :param lang: ISO code for supported language, Default laguage is english(en) 
        :type lang: string
        :return: server response in JSON object 
        """

        payload = {}

        if self.api_key is None:
            raise ValueError("Please provide your private API Key")

        # Check if valid data dictionary
        if data is not None:
            if is_valid_dict(data):
                payload["data"] = data
            else:
                raise TypeError("Data should be of type dictionary")
        else:
            raise ValueError("Please provide data, data can not be empty")

        # Check if valid language string
        if isinstance(lang, str):
            if lang in constants.SENTIMENT_LANGUAGES_SUPPORT:
                payload["lang"] = lang
            else:
                raise ValueError(
                    "Please provide valid Language code, check documentation for supported languages"
                )
        else:
            raise TypeError("Language input should be an string")

        # Make a POST request to constants.SENTIMENT_URL
        response = post(self.request_method, constants.SENTIMENT_URL,
                        self.header, payload, self.proxies,
                        self.request_timeout)

        if response.status_code == 500:
            response = MaxRetries(response, self.max_retries, self.retry_delay,
                                  self.request_method, constants.SENTIMENT_URL,
                                  self.header, payload, self.proxies,
                                  self.request_timeout)

        # Check the status code of the response if not equal to 200, then raise exception
        if response.status_code != 200:
            raise BytesviewException(response.json())

        # Return the response json
        return response.json()
Example #3
0
    def semantic_api(self, data=None, lang="en"):
        """ Sending POST request to the semantic api"""
        """
        :param data: Pass your both strings in the "string1" and "string2" key of the dictionary data. (ex. {"string1": "this is good", "string2": "this is great"})
        :type data: dictionary
        :param lang: Language Code (English - en), Default laguage is english(en) 
        :type lang: string
        :return: server response in JSON object 
        """

        payload = {}

        if self.api_key is None:
            raise ValueError("Please provide your private API Key")

        # Check if valid data dictionary
        if data is not None:
            if is_valid_dict(data):
                payload["data"] = data
            else:
                raise TypeError("Data should be of type dictionary")
        else:
            raise ValueError("Please provide data, data can not be empty")

        # Check if valid language string
        if isinstance(lang, str):
            if lang in constants.SEMANTIC_LANGUAGES_SUPPORT:
                payload["lang"] = lang
            else:
                raise ValueError(
                    "Please provide valid Language code, check documentation for supported languages"
                )
        else:
            raise TypeError("Language input should be an string")

        # Make a POST request to constants.SEMANTIC_URL
        response = self.request_method.post(constants.SEMANTIC_URL,
                                            auth=self.header,
                                            timeout=300,
                                            data=json.dumps(payload, indent=4))

        # Check the status code of the response if not equal to 200, then raise exception
        if response.status_code != 200:
            raise BytesviewException(response.json())

        # Return the response json
        return response.json()