get_categories.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

###########################
#######   LICENSE   #######
###########################

# Copyright (c) 2014-2016, Anodot Ltd. <info@anodot.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
#    following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
#    following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
#    products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import logging
import argparse
import requests
requests.packages.urllib3.disable_warnings()

#########################################################
# Function name: print_license                          #
# Params: None                                          #
# Returns: Anodot's license                             #
#########################################################
def print_license():
    text = '''
Copyright (c) 2014-2016, Anodot Ltd. <info@anodot.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
   following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
   following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
   products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    '''

    print(text)

#########################################################
# Function name: msg                                    #
# Params: None                                          #
# Returns: Returns the help message                     #
#########################################################
def msg(name=None):
    return '''get_categories.py
         Get the list of all categories.

         - Print Anodot's license: python get_categories.py --license
         - Get all categories: python get_categories.py --token <the API's token>

        '''


def main():
    logging.basicConfig(filename='get_categories.log',level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s', datefmt='%m/%d/%Y %H:%M:%S')
    logger = logging.getLogger(__name__)
    logger.info('Starting a run')

    parser = argparse.ArgumentParser(description='', usage=msg())
    parser.add_argument('--token', required=False, default=None, help="The Anodot's API token'")
    parser.add_argument('--license', required=False, action='store_true', help="Print Anodot's license")

    args = parser.parse_args()

    if(args.license):
        logger.info('user asked to print the license')
        print_license()
        exit()

    if(args.token is None):
        logger.error('--token is mandatory when getting all categories')
        raise Exception("--token is mandatory when getting all categories")
    else:
        logger.info('User asked to get all categories')
        logger.info('Sending http request to get all categories')
        response = requests.get('https://api.anodot.com/api/v1/user-events/categories?token=' + args.token, headers={'Content-Type': 'application/json'})
        if(response.status_code == 200):
            logger.info('Operation finished successfully')
            print "Listing all the categories: "
            body = response.content.replace("},", "}\n")
            print body
        else:
            logger.error('Getting all categories failed: ' + 'status code: ' + str(response.status_code) + ' response body: ' + response.content)
            print 'Getting all categories failed: ' + 'status code: ' + str(response.status_code) + ' response body: ' + response.content


if __name__ == "__main__":
    main()
 

Was this article helpful?
0 out of 0 found this helpful
-->