mirror of
https://github.com/CatalaLang/catala.git
synced 2024-11-09 22:16:10 +03:00
Differenciating API requests depending on text ID (beginning)
This commit is contained in:
parent
19ba5ce923
commit
09fc71c625
@ -12,6 +12,7 @@ depends: [
|
||||
"dune" {>= "2.8"}
|
||||
"ocaml" {>= "4.11.0"}
|
||||
"lwt" {>= "5.6.1"}
|
||||
"re" {>= "1.9.0"}
|
||||
"cohttp-lwt-unix" {>= "5.0.0"}
|
||||
"cohttp" {>= "5.0.0"}
|
||||
"tls" {>= "0.15.3"}
|
||||
|
@ -98,31 +98,72 @@ let make_request
|
||||
type article = Yojson.Basic.t
|
||||
|
||||
let run_request (request : (string * string t) t) : Yojson.Basic.t =
|
||||
let resp, body = Lwt_main.run request in
|
||||
let body = Lwt_main.run body in
|
||||
if resp = "200 OK" then (
|
||||
try body |> Yojson.Basic.from_string
|
||||
with Yojson.Basic.Util.Type_error (msg, obj) ->
|
||||
let try_once () =
|
||||
let resp, body = Lwt_main.run request in
|
||||
let body = Lwt_main.run body in
|
||||
resp, body
|
||||
in
|
||||
let handle_once resp body =
|
||||
if resp = "200 OK" then (
|
||||
try body |> Yojson.Basic.from_string
|
||||
with Yojson.Basic.Util.Type_error (msg, obj) ->
|
||||
Utils.Cli.error_print
|
||||
"Error while parsing JSON answer from API: %s\n\
|
||||
Specific JSON:\n\
|
||||
%s\n\
|
||||
Full answer:\n\
|
||||
%s"
|
||||
msg
|
||||
(Yojson.Basic.to_string obj)
|
||||
body;
|
||||
exit (-1))
|
||||
else raise (Failure "")
|
||||
in
|
||||
let resp, body = try_once () in
|
||||
try handle_once resp body
|
||||
with Failure _ -> (
|
||||
Utils.Cli.debug_format "Retrying request...";
|
||||
let resp, body = try_once () in
|
||||
try handle_once resp body
|
||||
with Failure _ ->
|
||||
Utils.Cli.error_print
|
||||
"Error while parsing JSON answer from API: %s\n\
|
||||
Specific JSON:\n\
|
||||
%s\n\
|
||||
Full answer:\n\
|
||||
%s"
|
||||
msg
|
||||
(Yojson.Basic.to_string obj)
|
||||
"The API request went wrong ; status is %s and the body is\n%s" resp
|
||||
body;
|
||||
exit (-1))
|
||||
else begin
|
||||
Utils.Cli.error_print
|
||||
"The API request went wrong ; status is %s and the body is\n%s" resp body;
|
||||
exit (-1)
|
||||
end
|
||||
|
||||
let retrieve_article (access_token : string) (article_id : string) :
|
||||
Yojson.Basic.t =
|
||||
type article_type = LEGIARTI | CETATEXT | JORFTEXT
|
||||
type article_id = { id : string; typ : article_type }
|
||||
|
||||
let parse_id (id : string) : article_id =
|
||||
let legi_rex =
|
||||
Re.(compile @@ whole_string @@ seq [str "LEGIARTI"; repn digit 12 None])
|
||||
in
|
||||
let ceta_tex =
|
||||
Re.(compile @@ whole_string @@ seq [str "CETATEXT"; repn digit 12 None])
|
||||
in
|
||||
let jorf_rex =
|
||||
Re.(compile @@ whole_string @@ seq [str "JORFTEXT"; repn digit 12 None])
|
||||
in
|
||||
let typ =
|
||||
if Re.execp legi_rex id then LEGIARTI
|
||||
else if Re.execp ceta_tex id then CETATEXT
|
||||
else if Re.execp jorf_rex id then JORFTEXT
|
||||
else
|
||||
Utils.Errors.raise_error
|
||||
"LégiFrance ID \"%s\" does not correspond to an ID format recognized \
|
||||
by the LégiFrance API"
|
||||
id
|
||||
in
|
||||
{ id; typ }
|
||||
|
||||
let retrieve_article (access_token : string) (obj : article_id) : Yojson.Basic.t
|
||||
=
|
||||
run_request
|
||||
(make_request access_token "consult/getArticle" ["id", article_id])
|
||||
(make_request access_token
|
||||
(match obj.typ with
|
||||
| CETATEXT -> "consult/juri"
|
||||
| _ -> "consult/getArticle")
|
||||
["id", obj.id])
|
||||
|
||||
let raise_article_parsing_error
|
||||
(json : Yojson.Basic.t)
|
||||
|
@ -31,10 +31,16 @@ val get_token : string -> string -> access_token
|
||||
API *)
|
||||
|
||||
type article
|
||||
type article_id
|
||||
|
||||
val retrieve_article : access_token -> string -> article
|
||||
val parse_id : string -> article_id
|
||||
(** [parse_id id] parses the string representing the LégiFrance object to be
|
||||
fetched from the API, checks its validity (for instance
|
||||
["LEGIARTI000006307920"]) and returns an [object_id]*)
|
||||
|
||||
val retrieve_article : access_token -> article_id -> article
|
||||
(** [retrieve_article token article_id] returns the article from the LegiFrance
|
||||
API. [article_id] should be of the form ["LEGIARTI000006307920"] *)
|
||||
API.*)
|
||||
|
||||
type law_excerpt
|
||||
|
||||
|
@ -25,7 +25,8 @@ let check_article_expiration
|
||||
(access_token : Api.access_token) : new_article_version option =
|
||||
match law_heading.Surface.Ast.law_heading_id with
|
||||
| None -> None
|
||||
| Some article_id ->
|
||||
| Some heading_id ->
|
||||
let article_id = Api.parse_id heading_id in
|
||||
let article = Api.retrieve_article access_token article_id in
|
||||
let api_article_expiration_date = Api.get_article_expiration_date article in
|
||||
let msg =
|
||||
@ -66,8 +67,8 @@ let check_article_expiration
|
||||
type law_article_text = {
|
||||
article_title : string * Utils.Pos.t;
|
||||
text : string;
|
||||
new_version : string option;
|
||||
current_version : string option;
|
||||
new_version : Api.article_id option;
|
||||
current_version : Api.article_id option;
|
||||
}
|
||||
(** Representation of the text of an article of law *)
|
||||
|
||||
@ -80,7 +81,7 @@ module Diff = Diff.Make (String)
|
||||
let compare_article_to_version
|
||||
(access_token : Api.access_token)
|
||||
(text : string)
|
||||
(version : string) : Diff.t option =
|
||||
(version : Api.article_id) : Diff.t option =
|
||||
let new_article = Api.retrieve_article access_token version in
|
||||
let new_article_text = Api.get_article_text new_article in
|
||||
let text_to_list text =
|
||||
@ -219,9 +220,9 @@ let rec traverse_source_code
|
||||
text = children_text;
|
||||
new_version =
|
||||
(match new_version with
|
||||
| Some (Available version) -> Some version
|
||||
| Some (Available version) -> Some (Api.parse_id version)
|
||||
| _ -> None);
|
||||
current_version = law_heading.law_heading_id;
|
||||
current_version = Option.map Api.parse_id law_heading.law_heading_id;
|
||||
}
|
||||
in
|
||||
if diff then compare_to_versions law_article_text access_token;
|
||||
|
Loading…
Reference in New Issue
Block a user