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)