1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . IO ;
4
+ using System . Threading . Tasks ;
5
+ using Bunq . Sdk . Context ;
6
+ using Bunq . Sdk . Exception ;
7
+ using Bunq . Sdk . Model . Core ;
8
+ using Bunq . Sdk . Model . Generated . Endpoint ;
9
+ using Mono . Options ;
10
+ using Tinker . Utils ;
11
+ using TinkerSrc . Lib ;
12
+
13
+ namespace TinkerSrc
14
+ {
15
+ public class TestOauth : ITinker
16
+ {
17
+ /// <summary>
18
+ /// Option constants.
19
+ /// </summary>
20
+ protected const string OPTION_AUTH_CODE = "code=" ;
21
+ protected const string OPTION_CLIENT_CONFIGURATION = "configuration=" ;
22
+ protected const string OPTION_REDIRECT = "redirect=" ;
23
+ protected const string OPTION_CONTEXT = "context=" ;
24
+
25
+ /// <summary>
26
+ /// API constants.
27
+ /// </summary>
28
+ protected const string API_DEVICE_DESCRIPTION = "##### YOUR DEVICE DESCRIPTION #####" ;
29
+
30
+ /// <summary>
31
+ /// Error constants.
32
+ /// </summary>
33
+ protected const string ERROR_MISSING_MANDATORY_OPTION = "Missing mandatory option." ;
34
+
35
+ public void Run ( string [ ] args )
36
+ {
37
+ Dictionary < string , string > allOption = AssertMandatoryOptions ( args ) ;
38
+
39
+ ApiContext apiContext = ApiContext . Restore ( allOption [ OPTION_CONTEXT ] ) ;
40
+ BunqContext . LoadApiContext ( apiContext ) ;
41
+
42
+ OauthAccessToken accessToken = OauthAccessToken . Create (
43
+ OauthGrantType . AUTHORIZATION_CODE ,
44
+ allOption [ OPTION_AUTH_CODE ] ,
45
+ allOption [ OPTION_REDIRECT ] ,
46
+ CreateOauthClientFromFile ( allOption [ OPTION_CLIENT_CONFIGURATION ] )
47
+ ) ;
48
+
49
+ apiContext = CreateApiContextByOauthToken (
50
+ accessToken ,
51
+ ShareLib . DetermineEnvironmentType ( args )
52
+ ) ;
53
+ BunqContext . LoadApiContext ( apiContext ) ;
54
+
55
+ ( new UserOverview ( ) ) . Run ( new String [ ] { } ) ;
56
+ }
57
+
58
+
59
+ private Dictionary < string , string > AssertMandatoryOptions ( string [ ] allArgument )
60
+ {
61
+ Dictionary < string , string > allOption = new Dictionary < string , string > ( ) ;
62
+
63
+ new OptionSet
64
+ {
65
+ { OPTION_AUTH_CODE , value => allOption . Add ( OPTION_AUTH_CODE , value ) } ,
66
+ { OPTION_REDIRECT , value => allOption . Add ( OPTION_REDIRECT , value ) } ,
67
+ { OPTION_CONTEXT , value => allOption . Add ( OPTION_CONTEXT , value ) } ,
68
+ { OPTION_CLIENT_CONFIGURATION , value => allOption . Add ( OPTION_CLIENT_CONFIGURATION , value ) }
69
+ } . Parse ( allArgument ) ;
70
+
71
+ if ( allOption [ OPTION_AUTH_CODE ] != null &&
72
+ allOption [ OPTION_REDIRECT ] != null &&
73
+ allOption [ OPTION_CONTEXT ] != null &&
74
+ allOption [ OPTION_CLIENT_CONFIGURATION ] != null ) {
75
+ return allOption ;
76
+ }
77
+
78
+ throw new BunqException ( ERROR_MISSING_MANDATORY_OPTION ) ;
79
+ }
80
+
81
+ private OauthClient CreateOauthClientFromFile ( String path )
82
+ {
83
+ return OauthClient . CreateFromJsonString (
84
+ File . ReadAllText ( path )
85
+ ) ;
86
+ }
87
+
88
+ private static ApiContext CreateApiContextByOauthToken ( OauthAccessToken token , ApiEnvironmentType environmentType )
89
+ {
90
+ return ApiContext . Create (
91
+ environmentType ,
92
+ token . Token ,
93
+ API_DEVICE_DESCRIPTION
94
+ ) ;
95
+ }
96
+ }
97
+ }
0 commit comments