text: import requests
  import json
  from .models import CarDealer, DealerReview
  from requests.auth import HTTPBasicAuth
  from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
  from ibm_watson import NaturalLanguageUnderstandingV1
  from ibm_watson.natural_language_understanding_v1 import Features,SentimentOptions
  import time
   
  
  def analyze_review_sentiments(text):
      url = "https://api.jp-tok.natural-language-understanding.watson.cloud.ibm.com/instances/18a08ec3-343b-4f72-b950-d1ddf3eb7aad"
      api_key = "J3Lbz2ygvj9PpUWaMR2tvUWmNyJK5L1Rgb-9ZCFTTppN"
      authenticator = IAMAuthenticator(api_key)
      natural_language_understanding = NaturalLanguageUnderstandingV1(version='2021-08-01',authenticator=authenticator)
      natural_language_understanding.set_service_url(url)
      response = natural_language_understanding.analyze( text=text+"hello hello hello",features=Features(sentiment=SentimentOptions(targets=[text+"hello hello hello"]))).get_result()
      label=json.dumps(response, indent=2)
      label = response['sentiment']['document']['label']
      
      
      return(label)
  
  
  def get_dealers_from_cf(url, **kwargs):
      results = []
      state = kwargs.get("state")
      if state:
          json_result = get_request(url, state=state)
      else:
          json_result = get_request(url)
  
      if json_result:
          # Get the row list in JSON as dealers
          dealers = json_result["body"]["rows"]
          # For each dealer object
          for dealer in dealers:
              # Get its content in `doc` object
              dealer_doc = dealer["doc"]
              # print(dealer_doc)
              # Create a CarDealer object with values in `doc` object
              dealer_obj = CarDealer(address=dealer_doc["address"], city=dealer_doc["city"],
                                     id=dealer_doc["id"], lat=dealer_doc["lat"], long=dealer_doc["long"],
                                  
                                     st=dealer_doc["st"], zip=dealer_doc["zip"])
              results.append(dealer_obj)
  
      return results
  
  
  def get_dealer_by_id_from_cf(url, id):
      json_result = get_request(url, id=id)
      # print(json_result)
      if json_result:
          dealers = json_result["body"]
          
      
          dealer_doc = dealers["docs"][0]
          dealer_obj = CarDealer(address=dealer_doc["address"], city=dealer_doc["city"],
                                  id=dealer_doc["id"], lat=dealer_doc["lat"], long=dealer_doc["long"],
                                  
                                  st=dealer_doc["st"], zip=dealer_doc["zip"])
      return dealer_obj
  
  
  def get_dealer_reviews_from_cf(url, **kwargs):
      results = []
      id = kwargs.get("id")
      if id:
          json_result = get_request(url, id=id)
      else:
          json_result = get_request(url)
      # print(json_result)
      if json_result:
          reviews = json_result["body"]["data"]["docs"]
          for dealer_review in reviews:
              review_obj = DealerReview(dealership=dealer_review["dealership"],
                                     name=dealer_review["name"],
                                     purchase=dealer_review["purchase"],
                                     review=dealer_review["review"])
              if "id" in dealer_review:
                  review_obj.id = dealer_review["id"]
              if "purchase_date" in dealer_review:
                  review_obj.purchase_date = dealer_review["purchase_date"]
              if "car_make" in dealer_review:
                  review_obj.car_make = dealer_review["car_make"]
              if "car_model" in dealer_review:
                  review_obj.car_model = dealer_review["car_model"]
              if "car_year" in dealer_review:
                  review_obj.car_year = dealer_review["car_year"]
              
              sentiment = analyze_review_sentiments(review_obj.review)
              print(sentiment)
              review_obj.sentiment = sentiment
              results.append(review_obj)
  
      return results
  
  
  def get_request(url, **kwargs):
      
      # If argument contain API KEY
      api_key = kwargs.get("api_key")
      print("GET from {} ".format(url))
      try:
          if api_key:
              params = dict()
              params["text"] = kwargs["text"]
              params["version"] = kwargs["version"]
              params["features"] = kwargs["features"]
              params["return_analyzed_text"] = kwargs["return_analyzed_text"]
              response = requests.get(url, params=params, headers={'Content-Type': 'application/json'},
                                      auth=HTTPBasicAuth('apikey', api_key))
          else:
              # Call get method of requests library with URL and parameters
              response = requests.get(url, headers={'Content-Type': 'application/json'},
                                      params=kwargs)
      except:
          # If any error occurs
          print("Network exception occurred")
  
      status_code = response.status_code
      print("With status {} ".format(status_code))
      json_data = json.loads(response.text)
      return json_data
  
  
  def post_request(url, payload, **kwargs):
      print(kwargs)
      print("POST to {} ".format(url))
      print(payload)
      response = requests.post(url, params=kwargs, json=payload)
      status_code = response.status_code
      print("With status {} ".format(status_code))
      json_data = json.loads(response.text)
      return json_data
  
  
  