1+ #if __ANDROID__
2+
3+ using System ;
4+ using System . Threading . Tasks ;
5+
6+ namespace Square . OkHttp3
7+ {
8+ public static class CallExtensions
9+ {
10+ public static Task < Response > ExecuteAsync ( this ICall call )
11+ {
12+ var tcs = new TaskCompletionSource < Response > ( ) ;
13+
14+ call . Enqueue (
15+ ( c , response ) =>
16+ {
17+ tcs . SetResult ( response ) ;
18+ } ,
19+ ( c , exception ) =>
20+ {
21+ if ( call . IsCanceled )
22+ {
23+ tcs . SetCanceled ( ) ;
24+ }
25+ else
26+ {
27+ tcs . SetException ( exception ) ;
28+ }
29+ } ) ;
30+
31+ return tcs . Task ;
32+ }
33+
34+ public static void Enqueue ( this ICall call , Action < ICall , Response > onResponse , Action < ICall , global ::Java . IO . IOException > onFailure )
35+ {
36+ call . Enqueue ( new ActionCallback ( onResponse , onFailure ) ) ;
37+ }
38+
39+ private class ActionCallback : global ::Java . Lang . Object , global ::Square . OkHttp3 . ICallback
40+ {
41+ private readonly Action < ICall , Response > onResponse ;
42+ private readonly Action < ICall , global ::Java . IO . IOException > onFailure ;
43+
44+ public ActionCallback ( Action < ICall , Response > onResponse , Action < ICall , global ::Java . IO . IOException > onFailure )
45+ {
46+ this . onResponse = onResponse ;
47+ this . onFailure = onFailure ;
48+ }
49+
50+ public void OnResponse ( ICall call , Response response )
51+ {
52+ if ( onResponse != null )
53+ {
54+ onResponse ( call , response ) ;
55+ }
56+ }
57+
58+ public void OnFailure ( ICall call , global ::Java . IO . IOException exception )
59+ {
60+ if ( onFailure != null )
61+ {
62+ onFailure ( call , exception ) ;
63+ }
64+ }
65+ }
66+ }
67+ }
68+
69+ #endif
0 commit comments