Execute an HTTP request defined with the ``meth`` callable and parse the response and interpret errors in compliance with SAPI REST interface. Note: For internal use only. Args: meth (callable): Callable object to be called wi
(meth, *args, **kwargs)
| 1885 | |
| 1886 | @staticmethod |
| 1887 | def _sapi_request(meth, *args, **kwargs): |
| 1888 | """Execute an HTTP request defined with the ``meth`` callable and |
| 1889 | parse the response and interpret errors in compliance with SAPI REST |
| 1890 | interface. |
| 1891 | |
| 1892 | Note: |
| 1893 | For internal use only. |
| 1894 | |
| 1895 | Args: |
| 1896 | meth (callable): |
| 1897 | Callable object to be called with args and kwargs supplied, with |
| 1898 | expected behavior consistent with one of ``requests.Session()`` |
| 1899 | request methods. |
| 1900 | |
| 1901 | *args, **kwargs (list, dict): |
| 1902 | Arguments to the ``meth`` callable. |
| 1903 | |
| 1904 | Returns: |
| 1905 | dict: JSON decoded body. |
| 1906 | |
| 1907 | Raises: |
| 1908 | A :class:`~dwave.cloud.exceptions.SAPIError` subclass, or |
| 1909 | :class:`~dwave.cloud.exceptions.RequestTimeout`. |
| 1910 | """ |
| 1911 | |
| 1912 | # execute request |
| 1913 | try: |
| 1914 | response = meth(*args, **kwargs) |
| 1915 | # note: LoggingSessionMixin will wrap timeout exceptions as RequestTimeout |
| 1916 | except api.exceptions.ResourceBadResponseError as e: |
| 1917 | # returned by VersionedAPISessionMixin; wrap for backwards-compatibility |
| 1918 | raise InvalidAPIResponseError(e) from e |
| 1919 | |
| 1920 | # workaround for charset_normalizer episode in requests>=2.26.0, |
| 1921 | # where decoding of an empty json object '{}' fails. |
| 1922 | # see: https://github.com/psf/requests/issues/5871, |
| 1923 | # https://github.com/dwavesystems/dwave-cloud-client/pull/471, and |
| 1924 | # https://github.com/dwavesystems/dwave-cloud-client/pull/476. |
| 1925 | response.encoding = 'utf-8' |
| 1926 | |
| 1927 | # NOTE: the expected behavior is for SAPI to return JSON error on |
| 1928 | # failure. However, that is currently not the case. We need to work |
| 1929 | # around this until it's fixed. |
| 1930 | |
| 1931 | # no error -> body is json |
| 1932 | # error -> body can be json or plain text error message |
| 1933 | if response.ok: |
| 1934 | try: |
| 1935 | return orjson.loads(response.content) |
| 1936 | except: |
| 1937 | raise InvalidAPIResponseError("JSON response expected") |
| 1938 | |
| 1939 | else: |
| 1940 | if response.status_code == 401: |
| 1941 | raise SolverAuthenticationError(error_code=401) |
| 1942 | |
| 1943 | try: |
| 1944 | msg = orjson.loads(response.content) |
no test coverage detected