diff --git a/lib/api/community/external.ml b/lib/api/community/external.ml index e69de29..b331121 100644 --- a/lib/api/community/external.ml +++ b/lib/api/community/external.ml @@ -0,0 +1,15 @@ +let proxy_steam_request steam_request_url profile_names game domain send = + match profile_names with + | [] -> Lwt.fail_with "Profile names list cannot be empty" + | _ids -> + let base_url = Uri.make ~scheme:"https" ~host:domain ~path:"/community/external/proxysteamuserrequest" () in + let url = + Uri.with_query' + base_url + [ "title", Data.Game.to_str game + ; "profile_names", Data.Query.encode_lst_s profile_names + ; "request", steam_request_url + ] + in + send url +;; diff --git a/lib/api/community/external.mli b/lib/api/community/external.mli new file mode 100644 index 0000000..8f94ed4 --- /dev/null +++ b/lib/api/community/external.mli @@ -0,0 +1,22 @@ +(** [proxy_steam_request] is a function that builds a request to a remote + Steam API proxy service. It constructs the URL with necessary parameters + and utilizes a provided function to send the request and return the + response as parsed JSON. + + @param steam_request_url The specific endpoint of the Steam API to be called. + @param profile_names A list of profile names to be included in the request. + @param game The specific game context for the request. + @param domain The domain of the remote API service. + @param send A function that takes a URI and returns a JSON response wrapped + in an Lwt option monad. + + @return + Returns a Yojson.Basic.t option wrapped in an Lwt monad, representing + the parsed JSON response, or None if there's an error or no data. *) +val proxy_steam_request + : string + -> string list + -> Data.Game.t + -> string + -> Data.Requester.Json.t + -> Yojson.Basic.t option Lwt.t diff --git a/tests/unit/res/proxysteamuserrequest.json b/tests/unit/res/proxysteamuserrequest.json new file mode 100644 index 0000000..0b1d0f1 --- /dev/null +++ b/tests/unit/res/proxysteamuserrequest.json @@ -0,0 +1,37 @@ +{ + "result": { + "code": 0, + "message": "SUCCESS" + }, + "avatars": [ + { + "profile_id": 196240, + "name": "/steam/76561197984749679", + "alias": "GL.TheViper", + "personal_statgroup_id": 200, + "xp": 5606, + "level": 3, + "leaderboardregion_id": 0, + "country": "de" + } + ], + "steamResults": { + "response": { + "players": [ + { + "steamid": "76561197984749679", + "communityvisibilitystate": 1, + "profilestate": 1, + "personaname": "GL.TheViper", + "commentpermission": 2, + "profileurl": "https://steamcommunity.com/id/secrettheviper/", + "avatar": "https://avatars.steamstatic.com/eefa125e4e662af9600355746783166942b8a1ff.jpg", + "avatarmedium": "https://avatars.steamstatic.com/eefa125e4e662af9600355746783166942b8a1ff_medium.jpg", + "avatarfull": "https://avatars.steamstatic.com/eefa125e4e662af9600355746783166942b8a1ff_full.jpg", + "avatarhash": "eefa125e4e662af9600355746783166942b8a1ff", + "personastate": 0 + } + ] + } + } +} diff --git a/tests/unit/test.ml b/tests/unit/test.ml index 2972852..93b5bef 100644 --- a/tests/unit/test.ml +++ b/tests/unit/test.ml @@ -23,6 +23,8 @@ let () = Test_cases.Api.test_get_leaderboard2 ()) ; Alcotest_lwt.test_case "/community/leaderboard/getInventoryByProfileIDs" `Quick (fun _ () -> Test_cases.Api.test_get_inventory ()) + ; Alcotest_lwt.test_case "/community/external/proxysteamuserrequest" `Quick (fun _ () -> + Test_cases.Api.test_proxy_request ()) ; Alcotest_lwt.test_case "invalid json" `Quick (fun _ () -> Test_cases.Api.test_invalid ()) ] ) ] diff --git a/tests/unit/test_cases/api.ml b/tests/unit/test_cases/api.ml index 3117733..823c5c6 100644 --- a/tests/unit/test_cases/api.ml +++ b/tests/unit/test_cases/api.ml @@ -113,6 +113,27 @@ let test_get_inventory () = | None -> Lwt.fail_with "Expected Some but got None" ;; +let test_proxy_request () = + let requester = Mock.Json_file.create_requester "proxysteamuserrequest.json" in + let client = Client.create "aoe-api.worldsedgelink.com" Data.Game.Age2 in + let endpoint = + Api.Community.External.proxy_steam_request "/ISteamUser/GetPlayerSummaries/v0002/" [ "/steam/76561197984749679" ] + in + Client.get endpoint client ~requester + >>= function + | Some json -> + (match json with + | `Assoc items -> + (match List.assoc_opt "result" items with + | Some (`Assoc result_items) -> + (match List.assoc_opt "message" result_items with + | Some (`String msg) -> Lwt.return @@ Alcotest.(check string) "Response was success" "SUCCESS" msg + | _ -> Lwt.fail_with "Unexpected JSON format: 'message' not found or wrong type") + | _ -> Lwt.fail_with "Unexpected JSON format: 'result' not found or wrong type") + | _ -> Lwt.fail_with "Unexpected JSON format: Top level is not an assoc") + | None -> Lwt.fail_with "Expected Some but got None" +;; + let test_invalid () = let endpoints = [ Api.Community.News.get ] in Lwt_list.iter_s (fun endpoint -> Util.request_with_file_throw endpoint) endpoints