Skip to content

Commit

Permalink
Add proxy request endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruhlmann committed Apr 12, 2024
1 parent fdd8ec4 commit 75a20d9
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/api/community/external.ml
Original file line number Diff line number Diff line change
@@ -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
;;
22 changes: 22 additions & 0 deletions lib/api/community/external.mli
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions tests/unit/res/proxysteamuserrequest.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
}
}
2 changes: 2 additions & 0 deletions tests/unit/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ())
] )
]
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test_cases/api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 75a20d9

Please sign in to comment.