From 109429fc3118a668ebb9b7e4311ece634a57a5e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kr=C3=BChlmann?= Date: Fri, 31 May 2024 17:05:05 +0200 Subject: [PATCH] Begin advertismenet members --- lib/api/community/news.ml | 5 ++- lib/api/game/advertisement.ml | 7 ++- lib/client.ml | 44 +++++++++---------- lib/{ => data}/json.ml | 0 lib/data/platform/cookie.ml | 12 +++-- lib/form.ml | 20 --------- .../game/observable_advertisment_member.ml | 16 +++++++ tests/integration/test.ml | 2 +- 8 files changed, 55 insertions(+), 51 deletions(-) rename lib/{ => data}/json.ml (100%) delete mode 100644 lib/form.ml create mode 100644 lib/models/stub/game/observable_advertisment_member.ml diff --git a/lib/api/community/news.ml b/lib/api/community/news.ml index 746b879..aba72c0 100644 --- a/lib/api/community/news.ml +++ b/lib/api/community/news.ml @@ -7,8 +7,9 @@ let get game domain send = match json with | Some j -> Lwt.return - @@ Json.try_parse_as - (module Models.Response.Community.News : Json.JsonParsable with type t = Models.Response.Community.News.t) + @@ Data.Json.try_parse_as + (module Models.Response.Community.News : Data.Json.JsonParsable + with type t = Models.Response.Community.News.t) j | None -> Lwt.return None ;; diff --git a/lib/api/game/advertisement.ml b/lib/api/game/advertisement.ml index e389c68..0520a15 100644 --- a/lib/api/game/advertisement.ml +++ b/lib/api/game/advertisement.ml @@ -24,6 +24,11 @@ let find_observable ?(start = 1) ?(count = 100) ?(sort = Descending) game domain in let* json = send url in match json with - | Some j -> Lwt.return @@ Some (Models.Response.Game.Observable_advertisements.from_json j) + | Some j -> + Lwt.return + @@ Data.Json.try_parse_as + (module Models.Response.Game.Observable_advertisements : Data.Json.JsonParsable + with type t = Models.Response.Game.Observable_advertisements.t) + j | None -> Lwt.return None ;; diff --git a/lib/client.ml b/lib/client.ml index 4b8e29f..04d3f2a 100644 --- a/lib/client.ml +++ b/lib/client.ml @@ -6,12 +6,25 @@ type t = ; cookie : Data.Platform.Cookie.t option } -let create ?(login = None) domain game = - match login with - | Some l -> - let* cookie = Data.Platform.Cookie.create l domain in - Lwt.return { domain; game; cookie } - | None -> Lwt.return { domain; game; cookie = None } +let create ?(login = None) ?(cookie = None) domain game = + match cookie with + | Some c -> + let* _ = Lwt_io.printl "Creating client using existing cookie" in + Lwt.return { domain; game; cookie = Some c } + | None -> + (match login with + | Some l -> + let* _ = Lwt_io.printl "Creating client using steam credentials" in + let* cookie = Data.Platform.Cookie.create l domain in + let* _ = + match cookie with + | Some c -> + Lwt_io.printl + @@ Printf.sprintf "Cookie created successfully '%s'" (Data.Platform.Cookie.to_cookie_string c) + | None -> Lwt_io.printl "Unable to create cookie" + in + Lwt.return { domain; game; cookie } + | None -> Lwt.return { domain; game; cookie = None }) ;; let get_json ?(cookie = None) (url : Uri.t) = @@ -24,15 +37,7 @@ let get_json ?(cookie = None) (url : Uri.t) = let headers = match cookie with | None -> Cohttp.Header.init () - | Some c -> - let cookies_string = - Printf.sprintf - "ApplicationGatewayAffinity=%s; ApplicationGatewayAffinityCORS=%s; reliclink=%s" - c.application_gateway_affinity - c.application_gateway_affinity_cors - c.reliclink - in - Cohttp.Header.add (Cohttp.Header.init ()) "Cookie" cookies_string + | Some c -> Cohttp.Header.add (Cohttp.Header.init ()) "Cookie" (Data.Platform.Cookie.to_cookie_string c) in let* resp, body = Cohttp_lwt_unix.Client.get ~headers url_with_params in let status = Cohttp.Response.status resp in @@ -40,14 +45,7 @@ let get_json ?(cookie = None) (url : Uri.t) = let curl_command = Printf.sprintf "curl -i -H 'Cookie: %s' '%s'" - (match cookie with - | Some c -> - Printf.sprintf - "ApplicationGatewayAffinity=%s; ApplicationGatewayAffinityCORS=%s; reliclink=%s" - c.application_gateway_affinity - c.application_gateway_affinity_cors - c.reliclink - | None -> "") + (match cookie with Some c -> Data.Platform.Cookie.to_cookie_string c | None -> "") (Uri.to_string url_with_params) in let* _ = Lwt_io.printl curl_command in diff --git a/lib/json.ml b/lib/data/json.ml similarity index 100% rename from lib/json.ml rename to lib/data/json.ml diff --git a/lib/data/platform/cookie.ml b/lib/data/platform/cookie.ml index 0736fb6..9ca23c3 100644 --- a/lib/data/platform/cookie.ml +++ b/lib/data/platform/cookie.ml @@ -6,10 +6,15 @@ type t = ; application_gateway_affinity_cors : string ; reliclink : string ; session_id : string - ; expires_on : int } -let get_seconds_until_expires c = c.expires_on - int_of_float (Unix.time ()) +let to_cookie_string cookie = + Printf.sprintf + "ApplicationGatewayAffinity=%s; ApplicationGatewayAffinityCORS=%s; reliclink=%s" + cookie.application_gateway_affinity + cookie.application_gateway_affinity_cors + cookie.reliclink +;; let make_form_data alias auth = [ "accountType", [ "STEAM" ] @@ -81,8 +86,7 @@ let create login domain = let application_gateway_affinity_cors = get_cookie_value cookies "ApplicationGatewayAffinityCORS" in let application_gateway_affinity = get_cookie_value cookies "ApplicationGatewayAffinity" in let reliclink = get_cookie_value cookies "reliclink" in - let expires_on = 3600 + int_of_float (Unix.time ()) in let session_id = extract_session_id body_str in - let c = { application_gateway_affinity; application_gateway_affinity_cors; reliclink; session_id; expires_on } in + let c = { application_gateway_affinity; application_gateway_affinity_cors; reliclink; session_id } in Lwt.return @@ Some c ;; diff --git a/lib/form.ml b/lib/form.ml deleted file mode 100644 index b62a1fc..0000000 --- a/lib/form.ml +++ /dev/null @@ -1,20 +0,0 @@ -open Lwt.Syntax -open Cohttp_lwt_unix - -let post (url : Uri.t) (form_data : (string * string) list) = - let headers = Cohttp.Header.init_with "Content-Type" "application/x-www-form-urlencoded" in - let body = Uri.encoded_of_query (List.map (fun (k, v) -> k, [ v ]) form_data) in - let* resp, body = Client.post ~headers ~body:(Cohttp_lwt.Body.of_string body) url in - let status = Cohttp.Response.status resp in - if Cohttp.Code.code_of_status status = 200 - then - let* body = Cohttp_lwt.Body.to_string body in - let json = Yojson.Basic.from_string body in - Lwt.return (Some json) - else ( - let url_str = Uri.to_string url in - let* _ = - Lwt_io.printl (Printf.sprintf "HTTP Error: %s for URL: %s" (Cohttp.Code.string_of_status status) url_str) - in - Lwt.return None) -;; diff --git a/lib/models/stub/game/observable_advertisment_member.ml b/lib/models/stub/game/observable_advertisment_member.ml new file mode 100644 index 0000000..4904b81 --- /dev/null +++ b/lib/models/stub/game/observable_advertisment_member.ml @@ -0,0 +1,16 @@ +type t = + { int1 : int + ; int2 : int + ; platform_id : string + ; icon : string option + ; name : string + ; string1 : string + ; int3 : int + ; int4 : int + ; int5 : int + ; int6 : int + ; int_null : int option + ; string2 : string + ; int7 : int option + ; list1 : Yojson.Basic.t list + } diff --git a/tests/integration/test.ml b/tests/integration/test.ml index 32c4e9e..f5c4e3a 100644 --- a/tests/integration/test.ml +++ b/tests/integration/test.ml @@ -23,10 +23,10 @@ let setup_community () = let setup_game () = let open Test_state.Game in let open Data.Platform.Steam_login in - let login = Some { alias = vgetenv "STEAM_USER_ALIAS"; app_ticket = vgetenv "STEAM_APP_TICKET" } in let domain = "aoe-api.worldsedgelink.com" in let game = Data.Game.Age2 in let endpoint = Api.Community.Leaderboard.get_leaderboard_2 ~count:1 in + let login = Some { alias = vgetenv "STEAM_USER_ALIAS"; app_ticket = vgetenv "STEAM_APP_TICKET" } in let* client = Client.create ~login domain game in let* leaderboards_response = Client.get endpoint client in match leaderboards_response with