예제 #1
0
    def __init__(self, settings=None, config_file='../config.ini', config_section='DEFAULT'):
        
        # Gets configuration information from config.ini. See ReadConfig
        # for more details.
        if not settings:
            settings = ReadConfig.main(config_section=config_section, file_name=config_file)
        if not settings:
            MakeConfig.main()
            settings = ReadConfig.main(config_section=config_section, file_name=config_file)

        # Set up the default HTTP request headers
        self.headers = {b'Accept': settings['accept'] }
        if settings['version']:
            self.headers['Version'] = settings['version']

        # Set up the security credentials. We can use either an encoded
        # username and password or a security token
        if 'auth_token' in settings:
            self.auth = {'SEC': settings['auth_token']}
        else:
            self.auth = {'Authorization': settings['authorization']}

        self.headers.update(self.auth)
        
        # Set up the server's ip address and the base URI that will be used for
        # all requests
        self.server_ip = settings['server_ip']
        self.base_uri = '/restapi/api/'
예제 #2
0
def main(args):

    # Gets settings dict by reading config.ini.
    settings = ReadConfig.main(file_name='config.ini')
    # If it doesn't exist, it calls on the user to input box information through
    # MakeConfig.main script.
    if not settings:
        MakeConfig.main(file_name='config.ini')
        settings = ReadConfig.main(file_name='config.ini')

    # Then if --print_api is true, then apiclient prints output of /help/capabilities
    # endpoint.
    if args[0].print_api:
        print_api(settings)
    # Then if --api and --method both have values, apiclient will attempt an api request.
    elif args[0].api and args[0].method:
        # Gets response object from making api call.
        response = make_request(args[0], settings)
        # Determines content type of response object (for printing).
        content_type = response.headers.get('Content-type')
        # Gleans body from response object.
        print(response.headers)
        body = response.read().decode('utf-8')
        output = body
        if response.code >= 300:
            #ERROR OCCURED, HANDLE ERROR
            [error_code, output] = handle_response_error(response, body)
        #SUCCESSFUL CALL
        # If JSON object, it pretty prints JSON
        # Else it merely prints the body of the response object.
        elif content_type == 'application/json':
            if body:
                try:
                    response_json = json.loads(body)
                    output = json.dumps(response_json, indent=2, separators=(',', ':'))
                except ValueError:
                    print("Failed to parse JSON, unparsed JSON below: ")
            else:
                print("\nResponse body was empty.\n")
        print(response.code)
        print("")
        print(output)

    # If either only api, or method args are sent, then then this error message is printed.
    else:
        message = ""
        if args[0].api:
            message += "httpMethod must be specified by --method argument\n"
        if args[0].method:
            message += "api endpoint must be specified by --api argument\n"
        if message:
            print("ArgumentError: " + message)
        print(USAGE_MESSAGE+"\n")
예제 #3
0
def main():

    # Gets parser to parse args.
    parser = get_parser()
    args = parser.parse_args()

    # Gets settings dict by reading config.ini.
    settings = ReadConfig.main(file_name='config.ini')
    # If it doesn't exist, it calls on the user to input box information through
    # MakeConfig.main script.
    if not settings:
        MakeConfig.main(file_name='config.ini')
        settings = ReadConfig.main(file_name='config.ini')

    # If -h, --help is true. Prints api help.
    if args[0].help:
        print_help(parser)
    # Then if --print_api is true, then apiclient prints output of /help/capabilities
    # endpoint.
    elif args[0].print_api:
        print_api(settings)
    # Then if --api and --method both have values, apiclient will attempt an api request.
    elif args[0].api and args[0].method:
        # Gets response object from making api call.
        response = make_request(args, settings)
        # Determines content type of response object (for printing).
        content_type = response.headers.get('Content-type')
        # Gleans body from response object.
        body = response.read().decode('utf-8')

        # If JSON object, it pretty prints JSON
        # Else it merely prints the body of the response object.
        if content_type == 'application/json':
            response_json = json.loads(body)
            print(json.dumps(response_json, indent=2, separators=(',', ':')))
        else:
            print(body)
    # If no args or incomplete args are sent, then print_help(parser) is called.
    else:
        message = ""
        if args[0].api:
            message += "httpMethod must be specified by --method argument\n"
        if args[0].method:
            message += "api endpoint must be specified by --api argument\n"
        if message:
            print("ArgumentError: " + message)
        print("Type 'python apiclient.py --help' for usage.\n")