MCPcopy
hub / github.com/hacs/integration / ProxyClientSession

Class ProxyClientSession

tests/common.py:447–495  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

445
446
447class ProxyClientSession(ClientSession):
448 response_mocker = ResponseMocker()
449
450 async def _request(self, method: str, str_or_url: StrOrURL, *args, **kwargs):
451 if str_or_url.startswith("ws://"):
452 return await super()._request(method, str_or_url, *args, **kwargs)
453
454 if (resp := self.response_mocker.get(str_or_url, args, kwargs)) is not None:
455 LOGGER.info("Using mocked response for %s", str_or_url)
456 if resp.exception:
457 raise resp.exception
458 return resp
459
460 url = URL(str_or_url)
461 fixture_file = f"fixtures/proxy/{url.host}{url.path}{'.json' if url.host in (
462 'api.github.com', 'data-v2.hacs.xyz') and not url.path.endswith('.json') else ''}"
463 fp = os.path.join(
464 os.path.dirname(__file__),
465 fixture_file,
466 )
467
468 LOGGER.info("Using mocked response from %s", fixture_file)
469
470 if not os.path.exists(fp):
471 raise Exception(f"Missing fixture for proxy/{url.host}{url.path}")
472
473 async def read(**kwargs):
474 if url.path.endswith(".zip"):
475 with open(fp, mode="rb") as fptr:
476 return fptr.read()
477 with open(fp, encoding="utf-8") as fptr:
478 return fptr.read().encode("utf-8")
479
480 async def json(**kwargs):
481 with open(fp, encoding="utf-8") as fptr:
482 return json_func.loads(fptr.read())
483
484 return MockedResponse(
485 url=url,
486 read=read,
487 json=json,
488 headers={
489 "X-RateLimit-Limit": "999",
490 "X-RateLimit-Remaining": "999",
491 "X-RateLimit-Reset": "999",
492 "Content-Type": "application/json",
493 "Etag": "321",
494 },
495 )
496
497
498async def client_session_proxy(hass: ha.HomeAssistant) -> ClientSession:

Callers

nothing calls this directly

Calls 1

ResponseMockerClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…