def test_get_request_with_list_param(m): client = ApiClient(user='******', password='******', host='https://databricks.com') with pytest.raises(AssertionError) as ex_info: client.perform_query('GET', '/endpoint', {'job_id': ['a', 'b']}) assert str(ex_info.value) == 'cannot pass list of objects'
def deploy(client: ApiClient, job_conf: Dict[str, Any], task_args: Dict[str, Any]): dbfs_new_jar_name = job_conf['libraries'][0]['jar'] logging.info("Submitting job with configuration %s and jar file %s" % (job_conf, dbfs_new_jar_name)) dbfs_api = DbfsApi(client) dbfs_api.cp(recursive=False, overwrite=True, src=task_args["jar"], dst=dbfs_new_jar_name) job_data = client.perform_query('POST', '/jobs/create', data=job_conf, headers=None) logging.info("Job creation data %s" % job_data) if task_args["run_now"]: logging.info("Requested to launch job immediately") run_data = client.perform_query('POST', '/jobs/run-now', data=job_data, headers=None) logging.info("Job launched with run data: %s" % run_data) if task_args["trace"]: logging.info("Requested to trace the job status") run_finised = False while not run_finised: time.sleep(4) run_status = client.perform_query('GET', '/jobs/runs/get', data={"run_id": run_data["run_id"]}, headers=None) logging.info(run_status) result_state = run_status["state"].get("result_state", None) if result_state: run_finised = True if result_state == "SUCCESS": logging.info("Job successfully finished!") else: exception_text = "Job finished with result state %s. Please check run UI!" % result_state raise Exception(exception_text) logging.info("All deployment actions successfully performed")
def test_no_content_from_server_on_error(m): m.get('https://databricks.com/api/2.0/endpoint', status_code=400, text='some html message') client = ApiClient(user='******', password='******', host='https://databricks.com') with pytest.raises(requests.exceptions.HTTPError): client.perform_query('GET', '/endpoint')
def test_content_from_server_on_error(m): data = {'cucumber': 'dade'} m.get('https://databricks.com/api/2.0/endpoint', status_code=400, text=json.dumps(data)) client = ApiClient(user='******', password='******', host='https://databricks.com') error_message_contains = "{'cucumber': 'dade'}" with pytest.raises(requests.exceptions.HTTPError) as e: client.perform_query('GET', '/endpoint') assert error_message_contains in e.value.message
def test_simple_request(m): data = {'cucumber': 'dade'} m.get('https://databricks.com/api/2.0/endpoint', text=json.dumps(data)) client = ApiClient(user='******', password='******', host='https://databricks.com') assert client.perform_query('GET', '/endpoint') == data
def test_get_request_with_float_param(m): data = {'cucumber': 'dade'} m.get('https://databricks.com/api/2.0/endpoint?job_id=0.25', text=json.dumps(data)) client = ApiClient(user='******', password='******', host='https://databricks.com') assert client.perform_query('GET', '/endpoint', {'job_id': 0.25}) == data
def test_get_request_with_false_param(m): data = {'cucumber': 'dade'} m.get('https://databricks.com/api/2.0/endpoint?active_only=false', text=json.dumps(data)) client = ApiClient(user='******', password='******', host='https://databricks.com') assert client.perform_query('GET', '/endpoint', {'active_only': False}) == data
def _submit_run(api_client: ApiClient, payload: Dict[str, Any]) -> Dict[str, Any]: return api_client.perform_query("POST", "/jobs/runs/submit", data=payload)
# Databricks notebook source from databricks_cli.sdk.api_client import ApiClient apiclient = ApiClient(token=dbutils.entry_point.getDbutils().notebook( ).getContext().apiToken().get(), host=dbutils.entry_point.getDbutils().notebook(). getContext().apiUrl().get()) # COMMAND ---------- apiclient.perform_query("GET", "/clusters/list?state=RUNNING") # COMMAND ---------- from databricks_cli.clusters.api import ClusterApi import json from pyspark.sql.types import * from pyspark.sql.functions import * # COMMAND ---------- clusters_api = ClusterApi(apiclient) # COMMAND ---------- data = clusters_api.list_clusters() rdd = sc.parallelize(data['clusters']).map(lambda x: json.dumps(x)) # # COMMAND ----------