Helper function to return an instantiated client, and solver, validating parameters in the process, while wrapping errors in `CLIError` and using `output` writer as a centralized printer.
(config, output=None)
| 351 | |
| 352 | |
| 353 | def _get_client_solver(config, output=None): |
| 354 | """Helper function to return an instantiated client, and solver, validating |
| 355 | parameters in the process, while wrapping errors in `CLIError` and using |
| 356 | `output` writer as a centralized printer. |
| 357 | """ |
| 358 | if output is None: |
| 359 | output = click.echo |
| 360 | |
| 361 | # get client |
| 362 | try: |
| 363 | client = Client.from_config(**config) |
| 364 | except Exception as e: |
| 365 | raise CLIError("Invalid configuration: {}".format(e), code=1) |
| 366 | |
| 367 | config_file = config.get('config_file') |
| 368 | if config_file: |
| 369 | output("Using configuration file: {config_file}", config_file=config_file) |
| 370 | |
| 371 | profile = config.get('profile') |
| 372 | if profile: |
| 373 | output("Using profile: {profile}", profile=profile) |
| 374 | |
| 375 | output("Using endpoint: {endpoint}", endpoint=client.config.endpoint) |
| 376 | output("Using region: {region}", region=client.config.region) |
| 377 | |
| 378 | # get solver |
| 379 | try: |
| 380 | solver = client.get_solver() |
| 381 | except SolverAuthenticationError: |
| 382 | raise CLIError("Authentication error. Check credentials in your configuration file.", 2) |
| 383 | except SolverNotFoundError: |
| 384 | raise CLIError("Solver not available.", 6) |
| 385 | except (InvalidAPIResponseError, UnsupportedSolverError) as e: |
| 386 | raise CLIError(f"Invalid or unexpected API response: {e!s}", 3) |
| 387 | except RequestTimeout: |
| 388 | raise CLIError("API connection timed out.", 4) |
| 389 | except requests.exceptions.SSLError as e: |
| 390 | # we need to handle `ssl.SSLError` wrapped in several exceptions, |
| 391 | # with differences between py2/3; greping the message is the easiest way |
| 392 | if 'CERTIFICATE_VERIFY_FAILED' in str(e): |
| 393 | raise CLIError( |
| 394 | "Certificate verification failed. Please check that your API endpoint " |
| 395 | "is correct. If you are connecting to a private or third-party D-Wave " |
| 396 | "system that uses self-signed certificate(s), please see " |
| 397 | "https://support.dwavesys.com/hc/en-us/community/posts/360018930954.", 5) |
| 398 | raise CLIError("Unexpected SSL error while fetching solver: {!r}".format(e), 5) |
| 399 | except Exception as e: |
| 400 | raise CLIError("Unexpected error while fetching solver: {!r}".format(e), 5) |
| 401 | |
| 402 | output("Using solver: {solver_name}", solver_name=solver.name) |
| 403 | if solver.identity.version: |
| 404 | output("Working graph version: {graph_id}", graph_id=solver.graph_id) |
| 405 | |
| 406 | return (client, solver) |
| 407 | |
| 408 | |
| 409 | def _sample(sample: Callable[[], Future], |
no test coverage detected