Finished cleanly isolating the API module

This commit is contained in:
Denis Merigoux 2020-05-22 23:33:40 +02:00
parent cd9957b8c4
commit ca6fd2173f
3 changed files with 64 additions and 46 deletions

View File

@ -109,17 +109,19 @@ let raise_article_parsing_error (json : Yojson.Basic.t) (msg : string) (obj : Yo
(Yojson.Basic.to_string obj) (Yojson.Basic.to_string json));
exit 1
let get_text_json (access_token : string) (text_id : string) : Yojson.Basic.t =
type law_excerpt = Yojson.Basic.t
let retrieve_law_excerpt (access_token : string) (text_id : string) : law_excerpt =
run_request (make_request access_token "consult/jorfPart" [ ("textCid", text_id) ])
let get_article_id (json : Yojson.Basic.t) : string =
let get_article_id (json : article) : string =
try
json
|> Yojson.Basic.Util.member "article"
|> Yojson.Basic.Util.member "id" |> Yojson.Basic.Util.to_string
with Yojson.Basic.Util.Type_error (msg, obj) -> raise_article_parsing_error json msg obj
let get_article_text (json : Yojson.Basic.t) : string =
let get_article_text (json : article) : string =
try
let text =
json
@ -137,7 +139,7 @@ let get_article_text (json : Yojson.Basic.t) : string =
text ^ " " ^ if nota <> "" then "NOTA : " ^ nota else ""
with Yojson.Basic.Util.Type_error (msg, obj) -> raise_article_parsing_error json msg obj
let get_article_expiration_date (json : Yojson.Basic.t) : Unix.tm =
let get_article_expiration_date (json : article) : Unix.tm =
try
let article_id = get_article_id json in
json
@ -153,7 +155,7 @@ let get_article_expiration_date (json : Yojson.Basic.t) : Unix.tm =
let date_compare (d1 : Unix.tm) (d2 : Unix.tm) : int =
int_of_float (fst (Unix.mktime d1)) - int_of_float (fst (Unix.mktime d2))
let get_article_new_version (json : Yojson.Basic.t) : string =
let get_article_new_version (json : article) : string =
let expiration_date = get_article_expiration_date json in
let get_version_date_debut (version : Yojson.Basic.t) : Unix.tm =
version
@ -171,3 +173,39 @@ let get_article_new_version (json : Yojson.Basic.t) : string =
date_compare (get_version_date_debut version1) (get_version_date_debut version2))
|> List.hd |> Yojson.Basic.Util.member "id" |> Yojson.Basic.Util.to_string
with Yojson.Basic.Util.Type_error (msg, obj) -> raise_article_parsing_error json msg obj
let get_law_excerpt_title (json : law_excerpt) : string =
json |> Yojson.Basic.Util.member "title" |> Yojson.Basic.Util.to_string
type law_excerpt_article = { id : string; num : string; content : string }
let clean_html (s : string) : string =
let new_line = Re.Pcre.regexp "\\s*\\<br\\s*\\/\\>\\s*" in
let s = Re.Pcre.substitute ~rex:new_line ~subst:(fun _ -> "\n") s in
let tag = Re.Pcre.regexp "\\<[^\\>]+\\>" in
let s = Re.Pcre.substitute ~rex:tag ~subst:(fun _ -> "") s in
String.trim s
let get_law_excerpt_articles (json : law_excerpt) : law_excerpt_article list =
let articles = json |> Yojson.Basic.Util.member "articles" |> Yojson.Basic.Util.to_list in
let articles =
List.sort
(fun a1 a2 ->
let a1_num =
int_of_string (a1 |> Yojson.Basic.Util.member "num" |> Yojson.Basic.Util.to_string)
in
let a2_num =
int_of_string (a2 |> Yojson.Basic.Util.member "num" |> Yojson.Basic.Util.to_string)
in
compare a1_num a2_num)
articles
in
List.map
(fun article ->
let article_id = article |> Yojson.Basic.Util.member "id" |> Yojson.Basic.Util.to_string in
let article_num = article |> Yojson.Basic.Util.member "num" |> Yojson.Basic.Util.to_string in
let article_content =
article |> Yojson.Basic.Util.member "content" |> Yojson.Basic.Util.to_string |> clean_html
in
{ id = article_id; num = article_num; content = article_content })
articles

View File

@ -20,20 +20,21 @@ type access_token
(** The [access_token] is the OAuth token used in every API request for authentication *)
val get_token : string -> string -> access_token
(** [get_token cliend_id client_secret] retrieves the access token from the LegiFrance API.
@see <https://developer.aife.economie.gouv.fr/> This is the official website of the French
government where you have to register to get your OAuth client ID and Secret for the LegiFrance
API *)
(** [get_token cliend_id client_secret] retrieves the access token from the LegiFrance API. You have
to register on the {{:https://developer.aife.economie.gouv.fr/} the official website of the
French government} to get your OAuth client ID and Secret for the LegiFrance API *)
type article
(** The type of law articles, returned by the LegiFrance API *)
val retrieve_article : access_token -> string -> article
(** [retrieve_article token article_id] returns the article from the LegiFrance API. [article_id]
should be of the form "LEGIARTI000006307920" *)
should be of the form ["LEGIARTI000006307920"] *)
val get_text_json : access_token -> string -> Yojson.Basic.t
type law_excerpt
val retrieve_law_excerpt : access_token -> string -> law_excerpt
(**[retrieve_law_excerpt token excerpt_id] returns a whole excerpt of a legislative statute from the
LegiFrance API. [excerpt_id] should be of the form ["JORFTEXT000033736934"] *)
(**{2 Manipulating API objects}*)
@ -46,3 +47,11 @@ val get_article_text : article -> string
val get_article_expiration_date : article -> Unix.tm
val get_article_new_version : article -> string
(**{3 Law excerpts}*)
val get_law_excerpt_title : law_excerpt -> string
type law_excerpt_article = { id : string; num : string; content : string }
val get_law_excerpt_articles : law_excerpt -> law_excerpt_article list

View File

@ -194,44 +194,15 @@ let compare_to_versions (article_text_acc : article_text_acc) (access_token : Ap
diff )
| None -> ()
let clean_html (s : string) : string =
let new_line = Re.Pcre.regexp "\\s*\\<br\\s*\\/\\>\\s*" in
let s = Re.Pcre.substitute ~rex:new_line ~subst:(fun _ -> "\n") s in
let tag = Re.Pcre.regexp "\\<[^\\>]+\\>" in
let s = Re.Pcre.substitute ~rex:tag ~subst:(fun _ -> "") s in
String.trim s
let include_legislative_text (id : string Catala.Pos.marked) (access_token : Api.access_token) :
unit =
let json = Api.get_text_json access_token (Catala.Pos.unmark id) in
let title =
"@@" ^ (json |> Yojson.Basic.Util.member "title" |> Yojson.Basic.Util.to_string) ^ "@@"
in
let articles = json |> Yojson.Basic.Util.member "articles" |> Yojson.Basic.Util.to_list in
let articles =
List.sort
(fun a1 a2 ->
let a1_num =
int_of_string (a1 |> Yojson.Basic.Util.member "num" |> Yojson.Basic.Util.to_string)
in
let a2_num =
int_of_string (a2 |> Yojson.Basic.Util.member "num" |> Yojson.Basic.Util.to_string)
in
compare a1_num a2_num)
articles
in
let excerpt = Api.retrieve_law_excerpt access_token (Catala.Pos.unmark id) in
let title = "@@" ^ Api.get_law_excerpt_title excerpt ^ "@@" in
let articles =
List.map
(fun article ->
let article_id = article |> Yojson.Basic.Util.member "id" |> Yojson.Basic.Util.to_string in
let article_num =
int_of_string (article |> Yojson.Basic.Util.member "num" |> Yojson.Basic.Util.to_string)
in
let article_content =
article |> Yojson.Basic.Util.member "content" |> Yojson.Basic.Util.to_string |> clean_html
in
Printf.sprintf "@Article %d|%s@\n%s" article_num article_id article_content)
articles
Printf.sprintf "@Article %s|%s@\n%s" article.Api.num article.Api.id article.Api.content)
(Api.get_law_excerpt_articles excerpt)
in
let to_insert = title ^ "\n\n" ^ String.concat "\n\n" articles in
let pos = Catala.Pos.get_position id in