def test_httpfs(): httpd = start_test_server(create_tmp_dir(prefix="http_fs_test"), port=8123) try: fs = HttpFs(read_url=Url.parse_or_raise("http://localhost:8123/")) eprint(" --> Opening some file...") with fs.openbin("/dir1/file1.txt", "r") as f: assert f.read() == "file1_contents".encode("ascii") eprint(" --> Verifying that opendir works nested dirs...") dir1 = fs.opendir("dir2") assert dir1.openbin("file2.txt", "r").read() == "file2_contents".encode("ascii") fs2 = HttpFs(read_url=Url.parse_or_raise("http://localhost:8123/dir1")) assert fs2.desc("file2.txt") == "http://localhost:8123/dir1/file2.txt" # check that "/" maps to the base url that was used to create the filesystem assert fs2.desc("/file2.txt") == "http://localhost:8123/dir1/file2.txt" #check that .. works even when creating the fs fs_updir = HttpFs( read_url=Url.parse_or_raise("http://localhost:8123/dir1/..")) with fs_updir.openbin("dir1/file1.txt", "r") as f: assert f.read() == "file1_contents".encode("ascii") finally: httpd.shutdown()
class JobProxyClient: API_URL: Url = Url.parse_or_raise( "https://unicore-job-proxy.apps.hbp.eu/api") def __init__(self, http_client_session: aiohttp.ClientSession, service_token: ServiceToken) -> None: self.http_client_session = http_client_session self.service_token = service_token super().__init__() async def start_job( self, *, job_def: JobDescription, site: SiteName, end_user_token: UserToken, ) -> "JobSubmission | Exception": payload: JsonValue = toJsonValue({ "job_def": job_def, "site": site.value, "user_info": end_user_token.access_token }) # print(f"Posting this payload:\n{json.dumps(payload, indent=4)}") resp = await self.http_client_session.post( self.API_URL.concatpath("jobs/").raw + "/", json=payload, headers={ "Authorization": f"Bearer {self.service_token.access_token}" }, ) if not resp.ok: return Exception( f"Request failed {await resp.text()}: {resp.text}") try: return JobSubmission.from_json_value(await resp.json()) except Exception as e: return e