def resolve_names(self): """Creates a dictionary representation of this object. This method converts an object to a dictionary that represents the format that the model should be in when passed into an API Request. Because of this, the generated dictionary may have different property names to that of the model itself. Returns: dict: The dictionary representing the object. """ # Create a mapping from Model property names to API property names replace_names = { "mtype": "type", "version": "version", "title": "title", "author_name": "author_name", "author_url": "author_url", "provider_name": "provider_name", "provider_url": "provider_url", "cache_age": "cache_age", "thumbnail_url": "thumbnail_url", "thumbnail_width": "thumbnail_width", "thumbnail_height": "thumbnail_height", "url": "url", "html": "html", "width": "width", "height": "height", } retval = dict() return APIHelper.resolve_names(self, replace_names, retval)
def resolve_names(self): """Creates a dictionary representation of this object. This method converts an object to a dictionary that represents the format that the model should be in when passed into an API Request. Because of this, the generated dictionary may have different property names to that of the model itself. Returns: dict: The dictionary representing the object. """ # Create a mapping from Model property names to API property names replace_names = { "status": "status", "object": "object", "details": "details", } retval = dict() return APIHelper.resolve_names(self, replace_names, retval)
def get_extract(self, url): """Does a GET request to /extract. The EmbedKit Extract API Args: url (string): The URL to extact the details from Returns: ExtractModel: Response from the API. The extracted details 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 += "/extract" # Process optional query parameters query_parameters = { "url": url, "api_key": self.__api_key } query_builder = APIHelper.append_url_with_query_parameters(query_builder, query_parameters) # Validate and preprocess url query_url = APIHelper.clean_url(query_builder) # Prepare headers headers = { "user-agent": "embedkit-sdk", "accept": "application/json", } # Prepare and invoke the API call request to fetch the response response = unirest.get(query_url, headers=headers) # Error handling using HTTP status codes if response.code == 429: raise APIException("Rate limit exceeded", 429, response.body) elif response.code == 400: raise APIException("Missing parameters", 400, response.body) elif response.code == 500: raise APIException("Unexpected error", 500, response.body) elif 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 ExtractModel(**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)