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
28 readEntity :: VecComp a => Response -> <Proc, Exception> a
29 readEntity response = readEntity_ response classObject
31 importJava "javax.ws.rs.client.Invocation$Builder" where
34 header :: Builder -> String -> String -> <Proc> Builder
37 acceptMediaType_ :: Builder -> Vector MediaType -> <Proc> Builder
39 @JavaName acceptEncoding
40 acceptEncoding_ :: Builder -> Vector String -> <Proc> Builder
42 @JavaName acceptLanguage
43 acceptLanguage_ :: Builder -> Vector String -> <Proc> Builder
44 cookie :: Builder -> String -> String -> <Proc> Builder
45 build :: Builder -> String -> <Proc> Invocation
46 buildGet :: Builder -> <Proc> Invocation
47 buildPost :: Builder -> Entity -> <Proc> Invocation
48 buildDelete :: Builder -> <Proc> Invocation
49 buildPut :: Builder -> Entity -> <Proc> Invocation
51 acceptMediaType :: Builder -> [MediaType] -> <Proc> Builder
52 acceptMediaType builder mediaTypes =
53 acceptMediaType_ builder $ vector mediaTypes
55 acceptEncoding :: Builder -> [String] -> <Proc> Builder
56 acceptEncoding builder encodings =
57 acceptEncoding_ builder $ vector encodings
59 acceptLanguage :: Builder -> [String] -> <Proc> Builder
60 acceptLanguage builder languages =
61 acceptEncoding_ builder $ vector languages
63 importJava "javax.ws.rs.client.WebTarget" where
66 path :: WebTarget -> String -> <Proc> WebTarget
67 request :: WebTarget -> <Proc> Builder
69 importJava "javax.ws.rs.client.ClientBuilder" where
73 clientBuilder :: <Proc> ClientBuilder
75 importJava "org.simantics.scl.rest.HttpClientUtils" where
76 buildClient :: ClientBuilder -> Client
77 statusMessageOf :: Response -> <Proc> String
78 asyncInvoke :: Invocation -> ResponseHandler -> FailureHandler -> <Proc> Future Response
79 trustAllClientBuilder :: <Proc> ClientBuilder
81 importJava "javax.ws.rs.client.Entity" where
85 formEntity :: Form -> <Proc> Entity
86 entity :: a -> MediaType -> <Proc> Entity
88 importJava "javax.ws.rs.client.Client" where
91 target :: Client -> String -> <Proc> WebTarget
92 close :: Client -> <Proc> ()
97 getExample :: String -> <Proc, Exception> String
99 httpClient = buildClient clientBuilder
100 webTarget = target httpClient uri
101 builder = request webTarget
102 acceptMediaType builder [WILDCARD_TYPE]
103 invocation = buildGet builder
104 response = syncInvoke invocation
105 print $ statusCodeOf response
106 print $ statusMessageOf response
107 res = readEntity response
111 postMultiPartExample :: String -> [File] -> <Proc, Exception> String
112 postMultiPartExample uri files = do
113 httpClient = buildClient clientBuilder
114 webTarget = target httpClient uri
115 builder = request webTarget
116 acceptMediaType builder [WILDCARD_TYPE]
117 mp = formDataMultiPart
118 addBodyPart mp $ formDataBodyPart "Name" "Test" $ withCharset TEXT_PLAIN_TYPE "utf-8"
120 addBodyPart mp $ fileDataBodyPart ("file" + show i) f APPLICATION_OCTET_STREAM_TYPE
122 invocation = buildPost builder $ entity mp MULTIPART_FORM_DATA_TYPE
123 response = syncInvoke invocation
124 print $ statusCodeOf response
125 print $ statusMessageOf response
126 res = readEntity response
130 postFileExample :: String -> File -> <Proc, Exception> String
131 postFileExample uri f = do
132 httpClient = buildClient clientBuilder
133 webTarget = target httpClient uri
134 builder = request webTarget
135 acceptMediaType builder [WILDCARD_TYPE]
136 mp = formDataMultiPart
137 invocation = buildPost builder $ entity f APPLICATION_OCTET_STREAM_TYPE
138 response = syncInvoke invocation
139 print $ statusCodeOf response
140 print $ statusMessageOf response
141 res = readEntity response
145 postFileAsyncExample :: String -> File -> <Proc, Exception> ()
146 postFileAsyncExample uri f = do
147 httpClient = buildClient clientBuilder
148 webTarget = target httpClient uri
149 builder = request webTarget
150 acceptMediaType builder [WILDCARD_TYPE]
151 mp = formDataMultiPart
152 invocation = buildPost builder $ entity f APPLICATION_OCTET_STREAM_TYPE
153 f = asyncInvoke invocation
155 print $ statusCodeOf response
156 print $ statusMessageOf response
158 content = ((readEntity response) :: String)
159 print $ length content
160 ) (\(t :: Throwable) -> print $ show t)
165 print $ show throwable
169 print $ isFutureDone f