-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2025 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module API.Proxy where

import Data.String.Conversions
import Network.HTTP.Client (Request (redirectCount), RequestBody (RequestBodyBS))
import Testlib.Prelude

getGiphy :: (HasCallStack, MakesValue caller) => caller -> String -> [(String, String)] -> App Response
getGiphy :: forall caller.
(HasCallStack, MakesValue caller) =>
caller -> String -> [(String, String)] -> App Response
getGiphy = String -> caller -> String -> [(String, String)] -> App Response
forall caller.
(HasCallStack, MakesValue caller) =>
String -> caller -> String -> [(String, String)] -> App Response
callGetProxy String
"giphy/v1/gifs/"

getYoutube :: (HasCallStack, MakesValue caller) => caller -> String -> [(String, String)] -> App Response
getYoutube :: forall caller.
(HasCallStack, MakesValue caller) =>
caller -> String -> [(String, String)] -> App Response
getYoutube = String -> caller -> String -> [(String, String)] -> App Response
forall caller.
(HasCallStack, MakesValue caller) =>
String -> caller -> String -> [(String, String)] -> App Response
callGetProxy String
"youtube/v3/"

getGoogleMaps :: (HasCallStack, MakesValue caller) => caller -> String -> [(String, String)] -> App Response
getGoogleMaps :: forall caller.
(HasCallStack, MakesValue caller) =>
caller -> String -> [(String, String)] -> App Response
getGoogleMaps = String -> caller -> String -> [(String, String)] -> App Response
forall caller.
(HasCallStack, MakesValue caller) =>
String -> caller -> String -> [(String, String)] -> App Response
callGetProxy String
"googlemaps/"

getSoundcloud :: (HasCallStack, MakesValue caller) => caller -> String -> [(String, String)] -> App Response
getSoundcloud :: forall caller.
(HasCallStack, MakesValue caller) =>
caller -> String -> [(String, String)] -> App Response
getSoundcloud = String -> caller -> String -> [(String, String)] -> App Response
forall caller.
(HasCallStack, MakesValue caller) =>
String -> caller -> String -> [(String, String)] -> App Response
callGetProxy String
"soundcloud/"

postSpotify :: (HasCallStack, MakesValue caller) => caller -> String -> String -> App Response
postSpotify :: forall caller.
(HasCallStack, MakesValue caller) =>
caller -> String -> String -> App Response
postSpotify = String -> caller -> String -> String -> App Response
forall caller.
(HasCallStack, MakesValue caller) =>
String -> caller -> String -> String -> App Response
callPostProxy String
"spotify/"

callPostProxy :: (HasCallStack, MakesValue caller) => String -> caller -> String -> String -> App Response
callPostProxy :: forall caller.
(HasCallStack, MakesValue caller) =>
String -> caller -> String -> String -> App Response
callPostProxy String
pathPrefix caller
caller String
path String
body = do
  Request
req <- caller -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest caller
caller Service
WireProxy Versioned
Unversioned (String
"/proxy/" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
pathPrefix String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
path)
  String -> Request -> App Response
submit String
"POST" (Request
req Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& RequestBody -> String -> Request -> Request
addBody (ByteString -> RequestBody
RequestBodyBS (String -> ByteString
forall a b. ConvertibleStrings a b => a -> b
cs String
body)) String
"application/json")

callGetProxy :: (HasCallStack, MakesValue caller) => String -> caller -> String -> [(String, String)] -> App Response
callGetProxy :: forall caller.
(HasCallStack, MakesValue caller) =>
String -> caller -> String -> [(String, String)] -> App Response
callGetProxy String
pathPrefix caller
caller String
path [(String, String)]
qparams = do
  Request
req <- caller -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest caller
caller Service
WireProxy Versioned
Unversioned (String
"/proxy/" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
pathPrefix String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
path)
  -- Otherwise, we would follow HTTP 302 responses...
  let req' :: Request
req' = Request
req {redirectCount = 0}
  String -> Request -> App Response
submit String
"GET" (Request
req' Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& [(String, String)] -> Request -> Request
addQueryParams [(String, String)]
qparams)