package ez_api
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
Easy API library and tools
Install
dune-project
Dependency
Authors
Maintainers
Sources
3.0.0.tar.gz
md5=9069f59ac83c958cedc90a25f24faa1e
sha512=4b066a759043e6704a9617bdc412d29f7ded058cb0e01eb3a8207f54b80bebb478bb98a96832e61b15d281bc8f132e1386459c563eb807a51c01324541fe28b2
doc/src/ez_api.request_lwt/ezRequest_lwt.ml.html
Source file ezRequest_lwt.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248(**************************************************************************) (* *) (* Copyright 2018-2023 OCamlPro *) (* *) (* All rights reserved. This file is distributed under the terms of the *) (* GNU Lesser General Public License version 2.1, with the special *) (* exception on linking described in the file LICENSE. *) (* *) (**************************************************************************) open EzAPI open EzRequest_common module type S = EzReq_lwt_S.S module type Interface = EzReq_lwt_S.Interface let (>|=) = Lwt.(>|=) let return = Lwt.return let request_reply_hook = ref (fun () -> ()) let before_hook = ref (fun () -> ()) let any_get = ref (fun ?meth:_m ?headers:_ ?msg:_ _url -> return (Error (-2, Some "No http client loaded")) ) let any_post = ref (fun ?meth:_m ?content_type:_ ?content:_ ?headers:_ ?msg:_ _url -> return (Error (-2, Some "No http client loaded")) ) module Make(S : Interface) : S = struct let init () = any_get := S.get; any_post := S.post let internal_get ?meth ?headers ?msg (URL url) = EzAPI.warnings (Format.eprintf "EzRequest.warning: %s"); let meth = match meth with None -> None | Some m -> Some ( String.uppercase_ascii @@ Meth.to_string m) in let headers = add_user_agent ?headers () in S.get ?meth ?headers ?msg url >|= fun code -> !request_reply_hook (); code let internal_post ?meth ?content_type ?content ?headers ?msg (URL url) = EzAPI.warnings (Format.eprintf "EzRequest.warning: %s"); let meth = match meth with None -> None | Some m -> Some ( String.uppercase_ascii @@ Meth.to_string m) in let headers = add_user_agent ?headers () in S.post ?meth ?content_type ?content ?headers ?msg url >|= fun code -> !request_reply_hook (); code let add_hook f = let old_hook = !before_hook in before_hook := (fun () -> old_hook (); f ()) let add_reply_hook f = let old_hook = !request_reply_hook in request_reply_hook := (fun () -> old_hook (); f ()) let get = internal_get let post = internal_post module Raw = struct type nonrec 'e api_error = 'e EzReq_lwt_S.api_error = | KnownError of { code : int ; error : 'e } | UnknownError of { code : int ; msg : string option } let decode_result io err_encodings = function | Error (code, None) -> Error (UnknownError { code ; msg = None }) | Error (code, Some msg) -> (match err_encodings ~code with | None -> Error (UnknownError { code ; msg = Some msg }) | Some encoding -> try Error ( KnownError { code ; error = EzEncoding.destruct encoding msg }) with _ -> Error (UnknownError { code ; msg = Some msg }) ) | Ok res -> match IO.from_string io (fun x -> x) res with | Ok s -> Ok s | Error (`destruct_exn exn) -> let msg = match exn with | Json_encoding.Cannot_destruct _ -> Json_encoding.print_error Format.str_formatter exn; Format.flush_str_formatter () | _ -> Printexc.to_string exn in Error (UnknownError { code = -3; msg = Some msg }) let handle_result service res = let err_encodings = Service.error service.s in let encoding = Service.output service.s in decode_result encoding err_encodings res let request ?headers ?params ?msg ?post ?url_encode ~input api service arg = !before_hook (); let meth = Service.meth service.s in handle_input ?params ?post ?url_encode ~input api service arg (internal_get ~meth ?headers ?msg, internal_post ~meth ?headers ?msg) >|= handle_result service let get0 ?post ?headers ?params ?msg api service = request ?headers ?params ?msg ?post ~input:() api service Req.dummy let get1 ?post ?headers ?params ?msg api service arg = request ?headers ?params ?msg ?post ~input:() api service (Req.dummy, arg) let get2 ?post ?headers ?params ?msg api service arg1 arg2 = request ?headers ?params ?msg ?post ~input:() api service ((Req.dummy, arg1), arg2) let post0 ?headers ?params ?msg ?url_encode ~input api service = request ?headers ?params ?msg ?url_encode ~input api service Req.dummy let post1 ?headers ?params ?msg ?url_encode ~input api service arg = request ?headers ?params ?msg ?url_encode ~input api service (Req.dummy, arg) let post2 ?headers ?params ?msg ?url_encode ~input api service arg1 arg2 = request ?headers ?params ?msg ?url_encode ~input api service ((Req.dummy, arg1), arg2) let handle_error kn = function | KnownError {code; error} -> code, kn error | UnknownError {code; msg} -> code, msg let string_of_error kn = function | KnownError {code; error} -> let content = match kn error with None -> "" | Some s -> ": " ^ s in Printf.sprintf "Error %d%s" code content | UnknownError {code; msg} -> let content = match msg with None -> "" | Some s -> ": " ^ s in Printf.sprintf "Unknown Error %d%s" code content let pp_error ?error pp = function | UnknownError {code; msg} -> Format.fprintf pp "Error %d%s" code (Option.fold ~none:"" ~some:(fun s -> ": " ^ s) msg) | KnownError {code; error=e} -> match error with | None -> Format.fprintf pp "Error %d" code | Some ppe -> Format.fprintf pp "Error %d: %a" code ppe e let wrap p = Lwt.map (Result.map_error (function | UnknownError {code; msg} -> code, `unknown msg | KnownError {code; error} -> code, `known error)) p end include Raw module Legacy = struct type _ api_error = int * string option type ('output, 'error, 'security) service0 = ('output) Legacy.service0 constraint 'security = [< Security.scheme ] type ('arg, 'output, 'error, 'security) service1 = ('arg, 'output) Legacy.service1 constraint 'security = [< Security.scheme ] type ('arg1, 'arg2, 'output, 'error, 'security) service2 = ('arg1, 'arg2, 'output) Legacy.service2 constraint 'security = [< Security.scheme ] type ('input, 'output, 'error, 'security) post_service0 = ('input, 'output) Legacy.post_service0 constraint 'security = [< Security.scheme ] type ('arg, 'input, 'output, 'error, 'security) post_service1 = ('arg, 'input, 'output) Legacy.post_service1 constraint 'security = [< Security.scheme ] type ('arg1, 'arg2, 'input, 'output, 'error, 'security) post_service2 = ('arg1, 'arg2, 'input, 'output) Legacy.post_service2 constraint 'security = [< Security.scheme ] type ('arg, 'input, 'output, 'error, 'security) service = (unit, 'arg, 'input, 'output) Legacy.service constraint 'security = [< Security.scheme ] open EzAPI.Legacy let unresultize = function | Ok res -> Ok res | Error UnknownError { code ; msg } -> Error (code, msg) | Error KnownError _ -> assert false (* Security.unreachable error *) let get0 ?post ?headers ?params ?msg api (service: 'output EzAPI.Legacy.service0) = get0 ?post ?headers ?params ?msg api service >|= unresultize let get1 ?post ?headers ?params ?msg api (service : ('arg,'output) service1) (arg : 'arg) = get1 ?post ?headers ?params ?msg api service arg >|= unresultize let get2 ?post ?headers ?params ?msg api (service : ('arg1, 'arg2, 'output) service2) (arg1 : 'arg1) (arg2 : 'arg2) = get2 ?post ?headers ?params ?msg api service arg1 arg2 >|= unresultize let post0 ?headers ?params ?msg ?url_encode ~(input : 'input) api (service : ('input,'output) post_service0) = post0 ?headers ?params ?msg ?url_encode ~input api service >|= unresultize let post1 ?headers ?params ?msg ?url_encode ~(input : 'input) api (service : ('arg, 'input,'output) post_service1) (arg : 'arg) = post1 ?headers ?params ?msg ?url_encode ~input api service arg >|= unresultize let post2 ?headers ?params ?msg ?url_encode ~(input : 'input) api (service : ('arg1, 'arg2, 'input, 'output) post_service2) (arg1 : 'arg1) (arg2 : 'arg2) = post2 ?headers ?params ?msg ?url_encode ~input api service arg1 arg2 >|= unresultize let request ?headers ?params ?msg ?post ?url_encode ~(input: 'input) api (service : (unit, 'arg, 'input, 'output) service) (arg : 'arg) = request ?headers ?params ?msg ?post ?url_encode ~input api service arg >|= unresultize let handle_error _ x = x let string_of_error _ (code, content) = let content = match content with None -> "" | Some s -> ": " ^ s in Printf.sprintf "Error %d%s" code content let pp_error ?error:_ pp (code, content) = Format.fprintf pp "Error %d%s" code (Option.fold ~none:"" ~some:(fun s -> ": " ^ s) content) let wrap p = Lwt.map (Result.map_error (fun (code, content) -> code, `unknown content)) p end end module ANY = Make(struct let get ?meth ?headers ?msg url = !any_get ?meth ?headers ?msg url let post ?meth ?content_type ?content ?headers ?msg url = !any_post ?meth ?content_type ?content ?headers ?msg url end)
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>