import "Vector" import "File" import "HTTP/Form" import "HTTP/MultiPart" import "HTTP/MediaType" import "Future" type ResponseHandler = Response -> () type FailureHandler = Throwable -> () NO_RESPONSE_HANDLER = (\response -> ()) :: ResponseHandler NO_FAILURE_HANDLER = (\failure -> ()) :: FailureHandler importJava "javax.ws.rs.client.Invocation" where data Invocation @JavaName invoke syncInvoke :: Invocation -> Response importJava "javax.ws.rs.core.Response" where data Response @JavaName getStatus statusCodeOf :: Response -> Integer @Private @JavaName readEntity readEntity_ :: Response -> Class a -> a @JavaName getHeaderString possibleHeaderOf :: Response -> String -> Maybe String readEntity :: VecComp a => Response -> a readEntity response = readEntity_ response classObject importJava "javax.ws.rs.client.Invocation$Builder" where data Builder header :: Builder -> String -> String -> Builder @private @JavaName accept acceptMediaType_ :: Builder -> Vector MediaType -> Builder @private @JavaName acceptEncoding acceptEncoding_ :: Builder -> Vector String -> Builder @private @JavaName acceptLanguage acceptLanguage_ :: Builder -> Vector String -> Builder cookie :: Builder -> String -> String -> Builder build :: Builder -> String -> Invocation buildGet :: Builder -> Invocation buildPost :: Builder -> Entity -> Invocation buildDelete :: Builder -> Invocation buildPut :: Builder -> Entity -> Invocation acceptMediaType :: Builder -> [MediaType] -> Builder acceptMediaType builder mediaTypes = acceptMediaType_ builder $ vector mediaTypes acceptEncoding :: Builder -> [String] -> Builder acceptEncoding builder encodings = acceptEncoding_ builder $ vector encodings acceptLanguage :: Builder -> [String] -> Builder acceptLanguage builder languages = acceptEncoding_ builder $ vector languages importJava "javax.ws.rs.client.WebTarget" where data WebTarget path :: WebTarget -> String -> WebTarget request :: WebTarget -> Builder importJava "javax.ws.rs.client.ClientBuilder" where data ClientBuilder @JavaName newBuilder clientBuilder :: ClientBuilder importJava "org.simantics.scl.rest.HttpClientUtils" where buildClient :: ClientBuilder -> Client statusMessageOf :: Response -> String asyncInvoke :: Invocation -> ResponseHandler -> FailureHandler -> Future Response trustAllClientBuilder :: ClientBuilder onReadProgress :: WebTarget -> (Long -> ()) -> () onWriteProgress :: WebTarget -> (Long -> ()) -> () possibleContentLengthOf :: Response -> Maybe Long importJava "javax.ws.rs.client.Entity" where data Entity @JavaName form formEntity :: Form -> Entity entity :: a -> MediaType -> Entity importJava "javax.ws.rs.client.Client" where data Client target :: Client -> String -> WebTarget close :: Client -> () /* Example usage: getExample :: String -> String getExample uri = do httpClient = buildClient clientBuilder webTarget = target httpClient uri builder = request webTarget acceptMediaType builder [WILDCARD_TYPE] invocation = buildGet builder response = syncInvoke invocation print $ statusCodeOf response print $ statusMessageOf response res = readEntity response close httpClient res postMultiPartExample :: String -> [File] -> String postMultiPartExample uri files = do httpClient = buildClient clientBuilder webTarget = target httpClient uri builder = request webTarget acceptMediaType builder [WILDCARD_TYPE] mp = formDataMultiPart addBodyPart mp $ formDataBodyPart "Name" "Test" $ withCharset TEXT_PLAIN_TYPE "utf-8" iterI (\i f -> addBodyPart mp $ fileDataBodyPart ("file" + show i) f APPLICATION_OCTET_STREAM_TYPE ) files invocation = buildPost builder $ entity mp MULTIPART_FORM_DATA_TYPE response = syncInvoke invocation print $ statusCodeOf response print $ statusMessageOf response res = readEntity response close httpClient res postFileExample :: String -> File -> String postFileExample uri f = do httpClient = buildClient clientBuilder webTarget = target httpClient uri builder = request webTarget acceptMediaType builder [WILDCARD_TYPE] invocation = buildPost builder $ entity f APPLICATION_OCTET_STREAM_TYPE response = syncInvoke invocation print $ statusCodeOf response print $ statusMessageOf response res = readEntity response close httpClient res postFileAsyncExample :: String -> File -> () postFileAsyncExample uri f = do httpClient = buildClient clientBuilder webTarget = target httpClient uri builder = request webTarget acceptMediaType builder [WILDCARD_TYPE] mp = formDataMultiPart invocation = buildPost builder $ entity f APPLICATION_OCTET_STREAM_TYPE f = asyncInvoke invocation (\response -> do print $ statusCodeOf response print $ statusMessageOf response catch (do content = ((readEntity response) :: String) print $ length content ) (\(t :: Throwable) -> print $ show t) close httpClient () ) (\throwable -> do print $ show throwable close httpClient () ) print $ isFutureDone f */