def get_token() -> str: """returns the necessary token for Updating PoleEmploi, or raise exceptions""" maj_pass_iae_api_scope = "passIAE api_maj-pass-iaev1" # The sandbox mode involves a slightly different scope if settings.API_ESD_MISE_A_JOUR_PASS_MODE != "production": maj_pass_iae_api_scope = "passIAE api_testmaj-pass-iaev1" # It is not obvious but we can ask for one token only with all the necessary rights token_recherche_et_maj = get_access_token( f"api_rechercheindividucertifiev1 rechercherIndividuCertifie {maj_pass_iae_api_scope}" ) return token_recherche_et_maj
def handle(self, **options): token = get_access_token("api_romev1 nomenclatureRome") url = f"{settings.API_ESD_BASE_URL}/rome/v1/metier" r = requests.get(url, headers={"Authorization": token}) r.raise_for_status() file_path = f"{CURRENT_DIR}/data/romes.json" with open(file_path, "wb") as f: f.write(r.content) self.stdout.write("-" * 80) self.stdout.write(f"File available at `{file_path}`.") self.stdout.write("Done.")
def handle(self, **options): result = {} with open(JSON_FILE, "r") as raw_json_data: json_data = json.load(raw_json_data) total_len = len(json_data) last_progress = 0 for i, item in enumerate(json_data): progress = int((100 * i) / total_len) if progress > last_progress + 5: self.stdout.write( f"Creating appellations for ROME codes… {progress}%") last_progress = progress rome_code = item["code"] self.stdout.write(f"Processing {rome_code}") token = get_access_token("api_romev1 nomenclatureRome") url = f"{settings.API_ESD_BASE_URL}/rome/v1/metier/{rome_code}/appellation" r = requests.get(url, headers={"Authorization": token}) r.raise_for_status() result[rome_code] = r.json() # Rate limiting is killing me. # We can't go down below 4 seconds without getting an error 429 "Too Many Requests". # Fetching this data is slow and can take between 35 to 40 min. time.sleep(4) file_path = f"{CURRENT_DIR}/data/appellations_for_rome.json" with open(file_path, "w") as f: json.dump(result, f) self.stdout.write("-" * 80) self.stdout.write(f"File available at `{file_path}`.") self.stdout.write("Done.")