X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.rest%2Fscl%2FHTTP%2FClient.scl;h=9ebeeea8f8907994ed5430d1a4ce1adc0f89afff;hp=76b0b63c4a59534ae58cded7adb27c47bc541a71;hb=69c49563a4bc5dff7d4e57541f15f384648f5b83;hpb=6fd9bc1ec7e95848d0cc15d12825a65a4b57ada5 diff --git a/bundles/org.simantics.scl.rest/scl/HTTP/Client.scl b/bundles/org.simantics.scl.rest/scl/HTTP/Client.scl index 76b0b63c4..9ebeeea8f 100644 --- a/bundles/org.simantics.scl.rest/scl/HTTP/Client.scl +++ b/bundles/org.simantics.scl.rest/scl/HTTP/Client.scl @@ -1,39 +1,89 @@ -import "Map" as Map import "Vector" -import "Logging" as LOGGER +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 - invoke_ :: Invocation -> Class a -> a + 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 -invoke :: VecComp a => Invocation -> a -invoke invocation = invoke_ invocation classObject +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 - accept :: Builder -> Vector 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 - queryParam :: WebTarget -> String -> Vector String -> WebTarget - request :: WebTarget -> Vector String -> Builder + request :: WebTarget -> Builder importJava "javax.ws.rs.client.ClientBuilder" where data ClientBuilder @JavaName newBuilder clientBuilder :: ClientBuilder - - @JavaName build + +importJava "org.simantics.scl.rest.HttpClientUtils" where buildClient :: ClientBuilder -> Client + statusMessageOf :: Response -> String + asyncInvoke :: Invocation -> ResponseHandler -> FailureHandler -> Future Response + trustAllClientBuilder :: ClientBuilder + +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 @@ -41,13 +91,80 @@ importJava "javax.ws.rs.client.Client" where target :: Client -> String -> WebTarget close :: Client -> () -getTextHtmlExample :: String -> Map.T String a -> String -getTextHtmlExample uri queryParams = do +/* +Example usage: + +getExample :: String -> String +getExample uri = do httpClient = buildClient clientBuilder webTarget = target httpClient uri - req = request webTarget (vector ["text/html"]) - invocation = build req "GET" - res = invoke invocation + 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] + mp = formDataMultiPart + 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 +*/ \ No newline at end of file