Browse Source

[metadata] Add json content type to the API

We do not advertize the new conf paramt in the hue.ini for now.
Romain Rigaux 10 năm trước cách đây
mục cha
commit
df0819d

+ 1 - 1
apps/spark/src/spark/job_server_api.py

@@ -98,7 +98,7 @@ class JobServerApi(object):
 
   def create_session(self, **properties):
     properties['proxyUser'] = self.user
-    return self._root.post('sessions', data=json.dumps(properties), contenttype='application/json')
+    return self._root.post('sessions', data=json.dumps(properties), contenttype=_JSON_CONTENT_TYPE)
 
   def get_session(self, uuid):
     return self._root.get('sessions/%s' % uuid)

+ 12 - 1
desktop/libs/metadata/src/metadata/conf.py

@@ -48,7 +48,7 @@ OPTIMIZER = ConfigSection(
       key="product_name",
       help=_t("The name of the product or group which will have API access to the emails associated with it."),
       private=True,
-      dynamic_default=get_auth_username),
+      default=None),
     PRODUCT_SECRET=Config(
       key="product_secret",
       help=_t("A secret passphrase associated with the productName."),
@@ -60,6 +60,17 @@ OPTIMIZER = ConfigSection(
       private=True,
       type=coerce_password_from_script,
       default=None),
+    PRODUCT_AUTH_SECRET=Config(
+      key="product_auth_secret",
+      help=_t("A secret passphrase associated with the productName."),
+      private=True,
+      dynamic_default=get_auth_password),
+    PRODUCT_AUTH_SECRET_SCRIPT=Config(
+      key="product_auth_secret_script",
+      help=_t("Execute this script to produce the product secret. This will be used when `product_secret` is not set."),
+      private=True,
+      type=coerce_password_from_script,
+      default=None),
 
     EMAIL=Config(
       key="email",

+ 11 - 7
desktop/libs/metadata/src/metadata/optimizer_client.py

@@ -31,8 +31,11 @@ from metadata.conf import OPTIMIZER
 LOG = logging.getLogger(__name__)
 
 
+_JSON_CONTENT_TYPE = 'application/json'
+
+
 def is_optimizer_enabled():
-  return OPTIMIZER.API_URL.get()
+  return OPTIMIZER.API_URL.get() and OPTIMIZER.PRODUCT_NAME.get()
 
 
 class OptimizerApiException(Exception):
@@ -41,24 +44,25 @@ class OptimizerApiException(Exception):
 
 class OptimizerApi(object):
 
-  def __init__(self, api_url=None, product_name=None, product_secret=None, ssl_cert_ca_verify=OPTIMIZER.SSL_CERT_CA_VERIFY.get()):
+  def __init__(self, api_url=None, product_name=None, product_secret=None, ssl_cert_ca_verify=OPTIMIZER.SSL_CERT_CA_VERIFY.get(), product_auth_secret=None):
     self._api_url = (api_url or OPTIMIZER.API_URL.get()).strip('/')
     self._product_name = product_name if product_name else OPTIMIZER.PRODUCT_NAME.get()
     self._product_secret = product_secret if product_secret else OPTIMIZER.PRODUCT_SECRET.get()
+    self._product_auth_secret = product_auth_secret if product_auth_secret else OPTIMIZER.PRODUCT_AUTH_SECRET.get()
 
     self._client = HttpClient(self._api_url, logger=LOG)
     self._client.set_verify(ssl_cert_ca_verify)
 
     self._root = resource.Resource(self._client)
 
-  def create_product(self, product_name, product_secret):
+  def create_product(self, product_name=None, product_secret=None, authCode=None):
     try:
       data = {
-          'productName': product_name,
-          'productSecret': product_secret,
-          'authCode': ''
+          'productName': product_name if product_name is not None else self._product_name,
+          'productSecret': product_secret if product_secret is not None else self._product_secret,
+          'authCode': authCode if authCode is not None else self._product_auth_secret
       }
-      return self._root.post('/api/createProduct', data)
+      return self._root.post('/api/createProduct', data=json.dumps(data), contenttype=_JSON_CONTENT_TYPE)
     except RestException, e:
       raise PopupException(e, title=_('Error while accessing Optimizer'))
 

+ 7 - 0
desktop/libs/metadata/src/metadata/optimizer_tests.py

@@ -57,7 +57,14 @@ class TestOptimizerApi(object):
     cls.user.save()
 
 
+  def test_api_create_product(self):
+    resp = self.api.create_product()
+
+    assert_equal('success', resp['status'], resp)
+
+
   def test_api_authenticate(self):
     resp = self.api.authenticate()
 
     assert_equal('success', resp['status'], resp)
+