@@ -14,127 +14,127 @@ class ApiArgumentParser(ArgumentParser):
1414    common command-line options for authenticating with the Janrain API and  
1515    allows for an janrain.capture.Api instance to be initialized using those 
1616    credentials. 
17-      
17+ 
1818    Example: 
19-          
19+ 
2020        parser = janrain.capture.cli.ApiArgumentParser() 
2121        args = parser.parse_args() 
2222        api = parser.init_api() 
23-          
23+ 
2424    """ 
2525    def  __init__ (self , * args , ** kwargs ):
2626        super (ApiArgumentParser , self ).__init__ (* args , ** kwargs )
2727        self ._parsed_args  =  None 
28-          
28+ 
2929        # credentials explicitly specified on the command line 
30-         self .add_argument ('-u' , '--apid_uri' ,  
30+         self .add_argument ('-u' , '--apid_uri' ,
3131                          help = "Full URI to the Capture API domain" )
3232        self .add_argument ('-i' , '--client-id' ,
3333                          help = "authenticate with a specific client_id" )
3434        self .add_argument ('-s' , '--client-secret' ,
3535                          help = "authenticate with a specific client_secret" )
36-          
36+ 
3737        # credentials defined in config file at the specified path 
3838        self .add_argument ('-k' , '--config-key' ,
3939                          help = "authenticate using the credentials defined at " \
4040                               "a specific path in the configuration file "     \
4141                               "(eg. clients.demo)" )
42-      
42+ 
4343        # default client found in the configuration file 
4444        self .add_argument ('-d' , '--default-client' , action = 'store_true' ,
4545                          help = "authenticate using the default client defined " \
46-                                "in the configuration file" )   
47-      
46+                                "in the configuration file" )
47+ 
4848    def  parse_args (self , args = None , namespace = None ):
4949        # override to store the result which can later be used by init_api() 
5050        args  =  super (ApiArgumentParser , self ).parse_args (args , namespace )
5151        self ._parsed_args  =  args 
5252        return  self ._parsed_args 
53-          
53+ 
5454    def  init_api (self , api_class = None ):
5555        """ 
5656        Initialize a janrain.capture.Api() instance for the credentials that 
57-         were specified on the command line or environment variables. This   
58-         method will use the first credentials it finds, looking in the   
57+         were specified on the command line or environment variables. This 
58+         method will use the first credentials it finds, looking in the 
5959        following order: 
60-          
60+ 
6161        1. A client_id and client_secret specified on the command line 
6262        2. A configuration key specified on the command line 
6363        3. The default client as specified with a flag on the command line 
64-         4. The CAPTURE_CLIENT_ID and CAPTURE_CLIENT_SECRET environment vars   
65-          
64+         4. The CAPTURE_CLIENT_ID and CAPTURE_CLIENT_SECRET environment vars 
65+ 
6666        Returns: 
6767            A janrain.capture.Api instance 
68-          
68+ 
6969        """ 
7070        if  not  self ._parsed_args :
7171            raise  Exception ("You must call the parse_args() method before "  \
7272                            "the init_api() method." )
73-              
73+ 
7474        args  =  self ._parsed_args 
75-          
75+ 
7676        if  args .client_id  and  args .client_secret :
7777            credentials  =  {
7878                'client_id' : args .client_id ,
7979                'client_secret' : args .client_secret 
8080            }
81-              
81+ 
8282        elif  args .config_key :
8383            credentials  =  config .get_settings_at_path (args .config_key )
84-              
84+ 
8585        elif  args .default_client :
8686            credentials  =  config .default_client ()
87-              
87+ 
8888        elif  'CAPTURE_CLIENT_ID'  in  os .environ  \
8989            and  'CAPTURE_CLIENT_SECRET'  in  os .environ :
9090            credentials  =  {
9191                'client_id' : os .environ ['CAPTURE_CLIENT_ID' ],
9292                'client_secret' : os .environ ['CAPTURE_CLIENT_SECRET' ]
9393            }
94-              
94+ 
9595        else :
9696            message  =  "You did not specify credentials to authenticate "  \
9797                      "with the Capture API." 
9898            raise  JanrainCredentialsError (message )
99-          
99+ 
100100        if  args .apid_uri :
101101            credentials ['apid_uri' ] =  args .apid_uri 
102-              
102+ 
103103        elif  'apid_uri'  not  in credentials :
104104            if  'CAPTURE_APID_URI'  in  os .environ :
105105                credentials ['apid_uri' ] =  os .environ ['CAPTURE_APID_URI' ]
106106            else :
107107                message  =  "You did not specify the URL to the Capture API" 
108108                raise  JanrainCredentialsError (message )
109-          
109+ 
110110        defaults  =  {k : credentials [k ] for  k  in  ('client_id' , 'client_secret' )}
111-          
111+ 
112112        if  api_class :
113113            return  api_class (credentials ['apid_uri' ], defaults )
114114        else :
115115            return  Api (credentials ['apid_uri' ], defaults )
116-          
116+ 
117117
118118def  main ():
119119    """ 
120120    Main entry point for CLI. This may be called by running the module directly 
121121    or by an executable installed onto the system path. 
122122    """ 
123123    parser  =  ApiArgumentParser (formatter_class = lambda  prog : HelpFormatter (prog ,max_help_position = 30 ))
124-     parser .add_argument ('api_call' ,  
124+     parser .add_argument ('api_call' ,
125125                        help = "API endpoint expressed as a relative path "  \
126126                             "(eg. /settings/get)." )
127-     parser .add_argument ('-p' , '--parameters' , nargs = '*' ,  
127+     parser .add_argument ('-p' , '--parameters' , nargs = '*' ,
128128                        metavar = "parameter=value" ,
129129                        help = "parameters passed through to the API call" )
130-     parser .add_argument ('-v' , '--version' , action = 'version' ,  
130+     parser .add_argument ('-v' , '--version' , action = 'version' ,
131131                        version = "capture-api "  +  get_version ())
132132    parser .add_argument ('-x' , '--disable-signed-requests' , action = 'store_true' ,
133133                        help = "sign HTTP requests" )
134134    parser .add_argument ('-b' , '--debug' , action = 'store_true' ,
135135                        help = "log debug messages to stdout" )
136136    args  =  parser .parse_args ()
137-      
137+ 
138138    try :
139139        api  =  parser .init_api ()
140140    except  (JanrainConfigError , JanrainCredentialsError ) as  error :
@@ -145,22 +145,22 @@ def main():
145145
146146    if  args .debug :
147147        logging .basicConfig (level = logging .DEBUG )
148-          
148+ 
149149    # map list of parameters from command line into a dict for use as kwargs 
150150    kwargs  =  {}
151151    if  args .parameters :
152-         kwargs  =  dict ((key , value ) for  key , value  in  [s .split ("=" , 1 )  
152+         kwargs  =  dict ((key , value ) for  key , value  in  [s .split ("=" , 1 )
153153                      for  s  in  args .parameters ])
154-      
154+ 
155155    try :
156156        data  =  api .call (args .api_call , ** kwargs )
157157    except  ApiResponseError  as  error :
158158        sys .exit ("API Error {} - {}\n " .format (error .code , error .message ))
159159    except  Exception  as  error :
160160        sys .exit ("Error - {}\n " .format (error ))
161-      
161+ 
162162    print (json .dumps (data , indent = 2 , sort_keys = True ))
163-      
163+ 
164164    sys .exit ()
165165
166166if  __name__  ==  "__main__" :
0 commit comments