2
2
3
3
import com .googlecode .totallylazy .Option ;
4
4
import com .googlecode .totallylazy .Sequence ;
5
+ import com .googlecode .totallylazy .Sequences ;
6
+ import com .googlecode .totallylazy .collections .Keyword ;
5
7
import com .googlecode .totallylazy .functions .Function1 ;
8
+ import com .googlecode .totallylazy .numbers .Integers ;
9
+ import com .googlecode .totallylazy .numbers .Numbers ;
6
10
import com .googlecode .utterlyidle .Response ;
7
11
import com .googlecode .utterlyidle .handlers .ClientHttpHandler ;
8
12
import javarepl .completion .CompletionResult ;
14
18
15
19
import static com .googlecode .totallylazy .Option .none ;
16
20
import static com .googlecode .totallylazy .Option .some ;
17
- import static com .googlecode .totallylazy .Sequences . sequence ;
21
+ import static com .googlecode .totallylazy .collections . Keyword . keyword ;
18
22
import static com .googlecode .totallylazy .json .Json .map ;
19
- import static com .googlecode .utterlyidle .RequestBuilder .get ;
20
- import static com .googlecode .utterlyidle .RequestBuilder .post ;
21
- import static javarepl .client .EvaluationLog .Type ;
23
+ import static com .googlecode .totallylazy .reflection .Types .classOf ;
24
+ import static com .googlecode .totallylazy .reflection .Types .parameterizedType ;
25
+ import static com .googlecode .utterlyidle .Request .get ;
26
+ import static com .googlecode .utterlyidle .Request .post ;
27
+ import static javarepl .client .EvaluationLog .Type .type ;
22
28
import static javarepl .completion .CompletionResult .methods .fromJson ;
23
29
import static javarepl .console .ConsoleStatus .Idle ;
30
+ import static javarepl .console .ConsoleStatus .consoleStatus ;
24
31
25
32
public final class JavaREPLClient {
33
+
34
+ public static final Keyword <String > EXPRESSION = keyword ("expression" , String .class );
35
+ public static final Keyword <String > TEMPLATE = keyword ("template" , String .class );
36
+ public static final Keyword <String > TOKEN = keyword ("token" , String .class );
37
+ public static final Keyword <String > TYPE = keyword ("type" , String .class );
38
+ public static final Keyword <String > MESSAGE = keyword ("message" , String .class );
39
+ public static final Keyword <String > STATUS = keyword ("status" , String .class );
40
+ public static final Keyword <String > VERSION = keyword ("version" , String .class );
41
+ public static final Keyword <String > VALUE = keyword ("value" , String .class );
42
+ public static final Keyword <String > POSITION = keyword ("position" , String .class );
43
+ public static final Keyword <List <String >> HISTORY = keyword ("history" , classOf (parameterizedType (List .class , String .class )));
44
+ public static final Keyword <List <String >> FORMS = keyword ("forms" , classOf (parameterizedType (List .class , String .class )));
45
+ public static final Keyword <List <Map <String , Object >>> LOGS = keyword ("logs" , classOf (parameterizedType (List .class , parameterizedType (Map .class , String .class , Object .class ))));
46
+ public static final Keyword <List <Map <String , Object >>> CANDIDATES = keyword ("candidates" , classOf (parameterizedType (List .class , parameterizedType (Map .class , String .class , Object .class ))));
47
+
26
48
private final String hostname ;
27
49
private final Integer port ;
28
50
private final ClientHttpHandler client ;
@@ -34,52 +56,56 @@ public JavaREPLClient(String hostname, Integer port) {
34
56
}
35
57
36
58
public synchronized ExpressionTemplate template (String expression ) throws Exception {
37
- Map <String , Object > model = map (client .handle (get (url ("template" )).query ("expression" , expression ).build ()).entity ().toString ());
38
- return new ExpressionTemplate (model .get ("template" ).toString (), model .get ("token" ).toString ());
59
+ Map <String , Object > response = map (client .handle (get (url ("template" )).query ("expression" , expression )).entity ().toString ());
60
+ String template = TEMPLATE .call (response );
61
+ String token = TOKEN .call (response );
62
+ return new ExpressionTemplate (template , token );
39
63
}
40
64
41
65
42
66
public synchronized Option <EvaluationResult > execute (String expr ) throws Exception {
43
- String json = client .handle (post (url ("execute" )).form ("expression" , expr ). build () ).entity ().toString ();
67
+ String json = client .handle (post (url ("execute" )).form ("expression" , expr )).entity ().toString ();
44
68
45
69
if (json .isEmpty ())
46
70
return none ();
47
71
48
- Map <String , Object > model = map (json );
49
- Sequence <Map <String , Object >> logs = sequence (( List < Object >) model . get ( "logs" )). unsafeCast ( );
50
- String expression = model . get ( "expression" ). toString ( );
72
+ Map <String , Object > response = map (json );
73
+ Sequence <Map <String , Object >> logs = LOGS . map ( Sequences :: sequence ). call ( response );
74
+ String expression = EXPRESSION . call ( response );
51
75
52
76
return some (new EvaluationResult (expression , logs .map (modelToEvaluationLog ())));
53
77
}
54
78
55
79
public synchronized CompletionResult completions (String expr ) throws Exception {
56
- return fromJson (client .handle (get (url ("completions" )).query ("expression" , expr ). build () ).entity ().toString ());
80
+ return fromJson (client .handle (get (url ("completions" )).query ("expression" , expr )).entity ().toString ());
57
81
}
58
82
59
83
public synchronized ConsoleStatus status () {
60
84
try {
61
- return ConsoleStatus .valueOf (map (client .handle (get (url ("status" )).build ()).entity ().toString ()).get ("status" ).toString ());
85
+ Map <String , Object > response = map (client .handle (get (url ("status" ))).entity ().toString ());
86
+ return consoleStatus (STATUS .call (response ));
62
87
} catch (Exception e ) {
63
88
return Idle ;
64
89
}
65
90
}
66
91
67
92
public synchronized String version () {
68
93
try {
69
- return map (client .handle (get (url ("version" )).build ()).entity ().toString ()).get ("version" ).toString ();
94
+ Map <String , Object > response = map (client .handle (get (url ("version" ))).entity ().toString ());
95
+ return VERSION .call (response );
70
96
} catch (Exception e ) {
71
97
return "[unknown]" ;
72
98
}
73
99
}
74
100
75
101
public synchronized Sequence <String > history () throws Exception {
76
- Response history = client .handle (get (url ("history" )). build () );
77
- Map <String , Object > model = map (history .entity ().toString ());
78
- return sequence (( List < Object >) model . get ( "history" )). safeCast ( String . class );
102
+ Response history = client .handle (get (url ("history" )));
103
+ Map <String , Object > response = map (history .entity ().toString ());
104
+ return HISTORY . map ( Sequences :: sequence ). call ( response );
79
105
}
80
106
81
107
private Function1 <Map <String , Object >, EvaluationLog > modelToEvaluationLog () {
82
- return model -> new EvaluationLog (Type . valueOf ( model . get ( "type" ). toString ()), model . get ( "message" ). toString ( ));
108
+ return response -> new EvaluationLog (type ( TYPE . call ( response )), MESSAGE . call ( response ));
83
109
}
84
110
85
111
private String url (String path ) {
0 commit comments