1 /**********************************************************\
2 |                                                          |
3 |                          hprose                          |
4 |                                                          |
5 | Official WebSite: http://www.hprose.com/                 |
6 |                   http://www.hprose.org/                 |
7 |                                                          |
8 \**********************************************************/
9 
10 /**********************************************************\
11  *                                                        *
12  * hprose/rpc/httpclient.d                                *
13  *                                                        *
14  * hprose http client library for D.                      *
15  *                                                        *
16  * LastModified: Jan 11, 2016                             *
17  * Author: Ma Bingyao <andot@hprose.com>                  *
18  *                                                        *
19 \**********************************************************/
20 
21 module hprose.rpc.httpclient;
22 
23 import hprose.rpc.client;
24 import vibe.http.client;
25 import vibe.stream.operations;
26 
27 class HttpClient: Client {
28     private {
29         HTTPClientSettings settings = new HTTPClientSettings;
30     }
31     alias settings this;
32     protected {
33         override ubyte[] sendAndReceive(ubyte[] request) {
34             return requestHTTP(uri, 
35                 (scope HTTPClientRequest req) {
36                     req.method = HTTPMethod.POST;
37                     req.writeBody(request, "application/hprose");
38                 },
39                 settings).bodyReader.readAll();
40         }
41         override void sendAndReceive(ubyte[] request, void delegate(ubyte[]) callback) {
42             requestHTTP(uri,
43                 (scope HTTPClientRequest req) {
44                     req.method = HTTPMethod.POST;
45                     req.writeBody(request, "application/hprose");
46                 },
47                 (scope HTTPClientResponse resp) {
48                     callback(resp.bodyReader.readAll());
49                 },
50                 settings);
51         }
52     }
53     this(string uri = "") {
54         super(uri);
55     }
56 }
57 
58 unittest {
59     import hprose.rpc.filter;
60     import hprose.rpc.context;
61     import hprose.rpc.common;
62     import std.stdio;
63 
64     interface Hello {
65         @Simple() @(ResultMode.Serialized) ubyte[] hello(string name);
66         @MethodName("hello") void asyncHello(string name, void delegate() callback = null);
67         void hello(string name, void delegate(string result) callback);
68         @(ResultMode.Serialized) void hello(string name, void delegate(ubyte[] result) callback);
69         void hello(string name, void delegate(string result, string name) callback);
70         void hello(string name, void delegate(string result, ref string name) callback);
71         void hello(string name, void function(string result) callback);
72         void hello(string name, void function(string result, string name) callback);
73         void hello(string name, void function(string result, ref string name) callback);
74     }
75     auto client = new HttpClient("http://hprose.com/example/index.php");
76     Hello proxy = client.useService!Hello();
77 //    client.filters ~= new class Filter {
78 //        override ubyte[] inputFilter(ubyte[] data, Context context) {
79 //            writeln(cast(string)data);
80 //            return data;
81 //        }
82 //
83 //        override ubyte[] outputFilter(ubyte[] data, Context context) {
84 //            writeln(cast(string)data);
85 //            return data;
86 //        };
87 //    };
88     string name = "world";
89     assert(proxy.hello("proxy sync") == cast(ubyte[])"s16\"Hello proxy sync\"");
90     proxy.asyncHello("proxy async");
91     proxy.hello("proxy async0", (string result) {
92             assert(result == "Hello proxy async0");
93         });
94     proxy.hello("proxy async1", (ubyte[] result) {
95             assert(result == cast(ubyte[])"s18\"Hello proxy async1\"");
96         });
97     proxy.hello("proxy async2", (result, arg0) {
98             assert(result == "Hello proxy async2");
99             assert(arg0 == "proxy async2");
100         });
101     proxy.hello("proxy async3", (result, ref arg0) {
102             assert(result == "Hello proxy async3");
103             assert(arg0 == "proxy async3");
104         });
105     proxy.hello("proxy async4", function(result) {
106             assert(result == "Hello proxy async4");
107         });
108     proxy.hello("proxy async5", function(result, arg0) {
109             assert(result == "Hello proxy async5");
110             assert(arg0 == "proxy async5");
111         });
112     proxy.hello("proxy async6", function(result, ref arg0) {
113             assert(result == "Hello proxy async6");
114             assert(arg0 == "proxy async6");
115         });
116 
117     assert(client.invoke!(string)("hello", "马秉尧") == "Hello 马秉尧");
118     client.invoke("hello", "async", () {});
119     client.invoke!(ResultMode.Serialized)("hello", "async1", "async1", delegate(ubyte[] result) {
120             assert(result == "s12\"Hello async1\"");
121         });
122     client.invoke("hello", "async2", "xxx", delegate(string result, string arg1, string arg2) {
123             assert(result == "Hello async2");
124             assert(arg1 == "async2");
125             assert(arg2 == "xxx");
126         });
127     client.invoke("hello", name, delegate(string result, string arg1) {
128             assert(result == "Hello " ~ name);
129             assert(arg1 == name);
130         });
131     client.invoke("hello", name, delegate(string result, ref string arg1) {
132             assert(result == "Hello " ~ name);
133             assert(arg1 == name);
134         });
135     client.invoke("hello", "async", function() {});
136     client.invoke("hello", "async3", function(string result) {
137             assert(result == "Hello async3");
138         });
139     client.invoke("hello", "async4", function(string result, string arg1) {
140             assert(result == "Hello async4");
141             assert(arg1 == "async4");
142         });
143     client.invoke("hello", name, function(string result, string arg1) {
144             assert(result == "Hello world");
145             assert(arg1 == "world");
146         });
147     client.invoke("hello", name, function(string result, ref string arg1) {
148             assert(result == "Hello world");
149             assert(arg1 == "world");
150         });
151 }