async def _pull_model_and_fingerprint(model_server: EndpointConfig, fingerprint: Optional[Text] ) -> Optional[(Text, Text)]: """Queries the model server and returns the value of the response's <ETag> header which contains the model hash. return None if not new fingerprint else return created temp directory and new fingerprint """ headers = {"If-None-Match": fingerprint} logger.debug("Requesting model from server {}..." "".format(model_server.url)) async with model_server.session() as session: try: params = model_server.combine_parameters() async with session.request("GET", model_server.url, timeout=DEFAULT_REQUEST_TIMEOUT, headers=headers, params=params) as resp: if resp.status in [204, 304]: logger.debug("Model server returned {} status code, " "indicating that no new model is available. " "Current fingerprint: {}" "".format(resp.status, fingerprint)) return None elif resp.status == 404: logger.debug( "Model server didn't find a model for our request. " "Probably no one did train a model for the project " "and tag combination yet.") return None elif resp.status != 200: logger.warning( "Tried to fetch model from server, but server response " "status code is {}. We'll retry later..." "".format(resp.status)) return None model_directory = tempfile.mkdtemp() utils.unarchive(await resp.read(), model_directory) logger.debug("Unzipped model to '{}'" "".format(os.path.abspath(model_directory))) # get the new fingerprint new_fingerprint = resp.headers.get("ETag") # return new fingerprint and new tmp model directory return model_directory, new_fingerprint except aiohttp.ClientResponseError as e: logger.warning("Tried to fetch model from server, but " "couldn't reach server. We'll retry later... " "Error: {}.".format(e)) return None
async def test_endpoint_config(): with aioresponses() as mocked: endpoint = EndpointConfig("https://example.com/", params={"A": "B"}, headers={"X-Powered-By": "Rasa"}, basic_auth={ "username": "******", "password": "******" }, token="mytoken", token_name="letoken", type="redis", port=6379, db=0, password="******", timeout=30000) mocked.post('https://example.com/test?A=B&P=1&letoken=mytoken', payload={"ok": True}, repeat=True, status=200) await endpoint.request("post", subpath="test", content_type="application/text", json={"c": "d"}, params={"P": "1"}) r = latest_request(mocked, 'post', "https://example.com/test?A=B&P=1&letoken=mytoken") assert r assert json_of_latest_request(r) == {"c": "d"} assert r[-1].kwargs.get("params", {}).get("A") == "B" assert r[-1].kwargs.get("params", {}).get("P") == "1" assert r[-1].kwargs.get("params", {}).get("letoken") == "mytoken" # unfortunately, the mock library won't report any headers stored on # the session object, so we need to verify them separately async with endpoint.session() as s: assert s._default_headers.get("X-Powered-By") == "Rasa" assert s._default_auth.login == "user" assert s._default_auth.password == "pass"