4 import "HTTP/MultiPart"
5 import "HTTP/MediaType"
8 type ResponseHandler = Response -> <Proc> ()
9 type FailureHandler = Throwable -> <Proc> ()
11 NO_RESPONSE_HANDLER = (\response -> ()) :: ResponseHandler
12 NO_FAILURE_HANDLER = (\failure -> ()) :: FailureHandler
14 importJava "javax.ws.rs.client.Invocation" where
18 syncInvoke :: Invocation -> <Proc, Exception> Response
20 importJava "javax.ws.rs.core.Response" where
23 statusCodeOf :: Response -> <Proc> Integer
26 readEntity_ :: Response -> Class a -> <Proc, Exception> a
27 @JavaName getHeaderString
28 possibleHeaderOf :: Response -> String -> <Proc> Maybe String
30 readEntity :: VecComp a => Response -> <Proc, Exception> a
31 readEntity response = readEntity_ response classObject
33 importJava "javax.ws.rs.client.Invocation$Builder" where
36 header :: Builder -> String -> String -> <Proc> Builder
39 acceptMediaType_ :: Builder -> Vector MediaType -> <Proc> Builder
41 @JavaName acceptEncoding
42 acceptEncoding_ :: Builder -> Vector String -> <Proc> Builder
44 @JavaName acceptLanguage
45 acceptLanguage_ :: Builder -> Vector String -> <Proc> Builder
46 cookie :: Builder -> String -> String -> <Proc> Builder
47 build :: Builder -> String -> <Proc> Invocation
48 buildGet :: Builder -> <Proc> Invocation
49 buildPost :: Builder -> Entity -> <Proc> Invocation
50 buildDelete :: Builder -> <Proc> Invocation
51 buildPut :: Builder -> Entity -> <Proc> Invocation
53 acceptMediaType :: Builder -> [MediaType] -> <Proc> Builder
54 acceptMediaType builder mediaTypes =
55 acceptMediaType_ builder $ vector mediaTypes
57 acceptEncoding :: Builder -> [String] -> <Proc> Builder
58 acceptEncoding builder encodings =
59 acceptEncoding_ builder $ vector encodings
61 acceptLanguage :: Builder -> [String] -> <Proc> Builder
62 acceptLanguage builder languages =
63 acceptEncoding_ builder $ vector languages
65 importJava "javax.ws.rs.client.WebTarget" where
68 path :: WebTarget -> String -> <Proc> WebTarget
69 request :: WebTarget -> <Proc> Builder
71 importJava "javax.ws.rs.client.ClientBuilder" where
75 clientBuilder :: <Proc> ClientBuilder
77 importJava "org.simantics.scl.rest.HttpClientUtils" where
78 buildClient :: ClientBuilder -> <Proc> Client
79 statusMessageOf :: Response -> <Proc> String
80 asyncInvoke :: Invocation -> ResponseHandler -> FailureHandler -> <Proc> Future Response
81 trustAllClientBuilder :: <Proc> ClientBuilder
82 onReadProgress :: WebTarget -> (Long -> <Proc> ()) -> <Proc> ()
83 onWriteProgress :: WebTarget -> (Long -> <Proc> ()) -> <Proc> ()
84 possibleContentLengthOf :: Response -> <Proc> Maybe Long
86 importJava "javax.ws.rs.client.Entity" where
90 formEntity :: Form -> <Proc> Entity
91 entity :: a -> MediaType -> <Proc> Entity
93 importJava "javax.ws.rs.client.Client" where
96 target :: Client -> String -> <Proc> WebTarget
97 close :: Client -> <Proc> ()
102 getExample :: String -> <Proc, Exception> String
104 httpClient = buildClient clientBuilder
105 webTarget = target httpClient uri
106 builder = request webTarget
107 acceptMediaType builder [WILDCARD_TYPE]
108 invocation = buildGet builder
109 response = syncInvoke invocation
110 print $ statusCodeOf response
111 print $ statusMessageOf response
112 res = readEntity response
116 postMultiPartExample :: String -> [File] -> <Proc, Exception> String
117 postMultiPartExample uri files = do
118 httpClient = buildClient clientBuilder
119 webTarget = target httpClient uri
120 builder = request webTarget
121 acceptMediaType builder [WILDCARD_TYPE]
122 mp = formDataMultiPart
123 addBodyPart mp $ formDataBodyPart "Name" "Test" $ withCharset TEXT_PLAIN_TYPE "utf-8"
125 addBodyPart mp $ fileDataBodyPart ("file" + show i) f APPLICATION_OCTET_STREAM_TYPE
127 invocation = buildPost builder $ entity mp MULTIPART_FORM_DATA_TYPE
128 response = syncInvoke invocation
129 print $ statusCodeOf response
130 print $ statusMessageOf response
131 res = readEntity response
135 postFileExample :: String -> File -> <Proc, Exception> String
136 postFileExample uri f = do
137 httpClient = buildClient clientBuilder
138 webTarget = target httpClient uri
139 builder = request webTarget
140 acceptMediaType builder [WILDCARD_TYPE]
141 invocation = buildPost builder $ entity f APPLICATION_OCTET_STREAM_TYPE
142 response = syncInvoke invocation
143 print $ statusCodeOf response
144 print $ statusMessageOf response
145 res = readEntity response
149 postFileAsyncExample :: String -> File -> <Proc, Exception> ()
150 postFileAsyncExample uri f = do
151 httpClient = buildClient clientBuilder
152 webTarget = target httpClient uri
153 builder = request webTarget
154 acceptMediaType builder [WILDCARD_TYPE]
155 mp = formDataMultiPart
156 invocation = buildPost builder $ entity f APPLICATION_OCTET_STREAM_TYPE
157 f = asyncInvoke invocation
159 print $ statusCodeOf response
160 print $ statusMessageOf response
162 content = ((readEntity response) :: String)
163 print $ length content
164 ) (\(t :: Throwable) -> print $ show t)
169 print $ show throwable
173 print $ isFutureDone f