package textrazor
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file client.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
open Cohttp open Lwt.Infix open Yojson.Safe type t = { api_key: string; secure: bool; use_eu_endpoint: bool } type response = (Yojson.Safe.t, string) result exception Invalid_item of string let create ?(secure=true) ?(use_eu_endpoint=false) api_key = {api_key; secure; use_eu_endpoint} let headers t = Header.of_list [("X-Textrazor-Key", t.api_key); ("Accept-Enconding", "gzip")] let parse_response (response, body) = let%lwt contents = Cohttp_lwt.Body.to_string body in let json = from_string contents in let result = if Response.status response |> Code.code_of_status |> Code.is_success then Ok json else Error (Util.member "error" json |> Util.to_string) in Lwt.return result let scheme t = if t.secure then "https" else "http" let host t = if t.use_eu_endpoint then "api-eu.textrazor.com" else "api.textrazor.com" let url path t = Uri.make ~scheme:(scheme t) ~host:(host t) ~path ?userinfo:None ?port:None ?fragment:None () let get path t = Lwt_main.run ( Cohttp_lwt_unix.Client.get (url path t) ~headers:(headers t) >>= parse_response ) let post_form path ?(params = []) t = Lwt_main.run ( Cohttp_lwt_unix.Client.post_form (url path t) ~headers:(headers t) ~params >>= parse_response )