delete_source.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 '''delete_source.py
         Delete a source by id.

         - Print Anodot's license: python delete_source.py --license
         - Create a category: python delete_source.py --token <the API's token> --id <the source id to be deleted>

        '''


def main():
    logging.basicConfig(filename='delete_source.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('--id', required=False, default=None, help="The source id to be deleted'")
    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 deleting a source')
        raise Exception("--token is mandatory when deleting a source")
    elif(args.id is None):
        logger.error('--id is mandatory when deleting a source')
        raise Exception("--id is mandatory when deleting a source")
    else:
        logger.info('User asked to delete a source')
        logger.info('Sending http request to delete a source')
        response = requests.delete('https://api.anodot.com/api/v1/user-events/sources/' + args.id + '?token=' + args.token, headers={'Content-Type': 'application/json'})
        if(response.status_code == 200):
            logger.info('Deleted source id ' + str(args.id) + " finished successfully")
            print 'Deleted source id ' + str(args.id) + " finished successfully"
        else:
            logger.error('Deleting source id ' + str(args.id) + ' failed. ' + ' status code: ' + str(response.status_code) + ' response body: ' + response.content)
            print 'Deleting source id ' + str(args.id) + ' 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
-->