Zapier'int'对象没有属性'copy'

问题描述 投票:0回答:1

我正在让我的zap向我的Snipe-IT数据库发出几个api请求。但是,当我尝试测试我的代码时,我收到以下错误:

'int'对象没有属性'copy'

enter image description here

当我检查我的Snipe-IT数据库时,一切都已经完成并且可以正常工作。 Zapier是唯一有问题的地方。我也检查了我的代码,并且copy这个词甚至没有列在其中。

我只是想知道是否有人知道为什么我得到一个错误甚至没有在我的代码中使用的东西,我可以做什么来解决它。

编辑2/8:

import requests  
import json  
import urllib  

if input_data.get('upgrade_response', '') == 'No change necessary, continue with the current hardware and agreement.':  
    return '', 200  

else:  
    #print(request.json)  

    #print('Loading Variables...')  

    #This is the API key for the user in Snipe-IT  
    snipe_it_api_key = 'api_key'  

    #This checks the dict key 'customer_name' and if the key does not exist it enters '. .' in as the value  
    #This checks to see if the last character in the value is ' '  
    #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'customer_name'  
    if input_data.get('customer_name', '. .').replace('\n', ' ').replace('"', '')[-1] == ' ':  
        customer_name = input_data.get('customer_name', '. .').replace('\n', ' ').replace('"', '')[:-1]  

    #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'customer_name'  
    else:  
        customer_name = input_data.get('customer_name', '. .').replace('\n', ' ').replace('"', '')  


    #This splits the variable 'customer_name' into a list at any ' '  
    customer_name_split = customer_name.split()  

    #This grabs the first item in the list and saves it as the variable 'customer_name_first'  
    customer_name_first = customer_name_split[0]  

    #This grabs the last item in the list and saves it as the variable 'customer_name_last'  
    customer_name_last = customer_name_split[-1]  


    #This checks the dict key 'customer_name' and if the key does not exist it enters '' in as the value  
    #This checks to see if the last character in the value is ' '  
    #If this statement is true, it goes through and removes the last character at the end of the value then   
    #it checks for and removes any "’" "'" or '"' that are found in the value and it checks for and replaces any '\n' with ' ' that are found in the value then saves it as the variable 'company_name'  
    if input_data.get('company_name', '').replace('\n', ' ').replace('"', '')[-1] == ' ':  
        company_name = input_data.get('company_name', '').replace('\n', ' ').replace('"', '').replace("'", '').replace("`", '').replace("’", '')[:-1]  

    #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'company_name'
    else:
        company_name = input_data.get('company_name', '').replace('\n', ' ').replace('"', '').replace("'", '').replace("`", '').replace("’", '')


    #This checks the dict key 'name_first' and if the key does not exist it enters '' in as the value
    #This checks to see if the last character in the value is ' '
    #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'name_first'
    if (input_data.get('name_first', '').replace('\n', ' ').replace('"', '') + customer_name_first.replace('.', ''))[-1] == ' ':
        name_first = (input_data.get('name_first', '').replace('\n', ' ').replace('"', '') + customer_name_first.replace('.', ''))[:-1]

    #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'name_first'
    else:
        name_first = input_data.get('name_first', '').replace('\n', ' ').replace('"', '') + customer_name_first.replace('.', '')


    #This checks the dict key 'name_last' and if the key does not exist it enters '' in as the value
    #This checks to see if the last character in the value is ' '
    #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'name_last'
    if (input_data.get('name_last', '').replace('\n', ' ').replace('"', '') + customer_name_last.replace('.', ''))[-1] == ' ':
        name_last = (input_data.get('name_last', '').replace('\n', ' ').replace('"', '') + customer_name_last.replace('.', ''))[:-1]

    #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'name_last'
    else:
        name_last = input_data.get('name_last', '').replace('\n', ' ').replace('"', '') + customer_name_last.replace('.', '')


    #This checks the dict key 'alias' and if the key does not exist it enters '' in as the value
    #This checks to see if the last character in the value is ' '
    #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'alias'
    if input_data.get('alias', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
        alias = input_data.get('alias', '').replace('\n', ' ').replace('"', '')[:-1]

    #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'alias'
    else:
        alias = input_data.get('alias', '').replace('\n', ' ').replace('"', '')


    #This checks the dict key 'customer_id' and if the key does not exist it enters '' in as the value
    #This checks to see if the last character in the value is ' '
    #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'customer_id'
    if input_data.get('customer_id', ' ').replace('\n', ' ').replace('"', '')[-1] == ' ':
        customer_id = input_data.get('customer_id', '').replace('\n', ' ').replace('"', '')[:-1]

    #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'customer_id'
    else:
        customer_id = input_data.get('customer_id', '').replace('\n', ' ').replace('"', '')


    #This checks the dict key 'phone_number' and if the key does not exist it enters '' in as the value
    #This checks to see if the last character in the value is ' '
    #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' or '-' or '(' or ')' that are found in the value then saves it as the variable 'phone_number'
    if input_data.get('phone_number', '').replace('\n', ' ').replace('"', '').replace('-', '').replace('(', '').replace(')', '')[-1] == ' ':
        phone_number = input_data.get('phone_number', '').replace('\n', ' ').replace('"', '').replace('-', '').replace('(', '').replace(')', '')[:-1]

    #If the statement is false, it checks for and changes any '\n' or '"' or '-' or '(' or ')' that are found in the value then saves it as the variable 'phone_number'
    else:
        phone_number = input_data.get('phone_number', '').replace('\n', ' ').replace('"', '').replace('-', '').replace('(', '').replace(')', '')


    #This checks the dict key 'install_address' and if the key does not exist it enters '' in as the value
    #If this statement is true it will use the dict key 'location_address'
    if input_data.get('install_address', '') == '':

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_address'
        if input_data.get('location_address', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            location_address = input_data.get('location_address', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_address'
        else:
            location_address = input_data.get('location_address', '').replace('\n', ' ').replace('"', '')

    #If the statement is false it will use the dict key 'install_address'
    else:

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_address'
        if input_data.get('install_address', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            location_address = input_data.get('install_address', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_address'
        else:
            location_address = input_data.get('install_address', '').replace('\n', ' ').replace('"', '')


    #This checks the dict key 'install_city' and if the key does not exist it enters '' in as the value
    #If this statement is true it will use the dict key 'location_city'
    if input_data.get('install_city', '') == '':

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_city'
        if input_data.get('location_city', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            location_city = input_data.get('location_city', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_city'
        else:
            location_city = input_data.get('location_city', '').replace('\n', ' ').replace('"', '')

    #If the statement is false it will use the dict key 'install_city'
    else:

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_city'
        if input_data.get('install_city', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            location_city = input_data.get('install_city', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_city'
        else:
            location_city = input_data.get('install_city', '').replace('\n', ' ').replace('"', '')


    #This checks the dict key 'install_state' and if the key does not exist it enters '' in as the value
    #If this statement is true it will use the dict key 'location_state'
    if input_data.get('install_state', '') == '':

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_state'
        if input_data.get('location_state', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            location_state = input_data.get('location_state', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_state'
        else:
            location_state = input_data.get('location_state', '').replace('\n', ' ').replace('"', '')

    #If the statement is false it will use the dict key 'install_state'
    else:

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_state'
        if input_data.get('install_state', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            location_state = input_data.get('install_state', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_state'
        else:
            location_state = input_data.get('install_state', '').replace('\n', ' ').replace('"', '')


    #This checks the dict key 'install_country_code' and if the key does not exist it enters '' in as the value
    #If this statement is true it will use the dict key 'country_code'
    if input_data.get('install_country_code', '') == '':

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'country_code'
        if input_data.get('country_code', ' ').replace('\n', ' ').replace('"', '')[-1] == ' ':
            country_code = input_data.get('country_code', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'country_code'
        else:
            country_code = input_data.get('country_code', '').replace('\n', ' ').replace('"', '')

    #If the statement is false it will use the dict key 'install_country_code'
    else:

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'country_code'
        if input_data.get('install_country_code', '') == '':
            country_code = input_data.get('install_country_code', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'country_code'
        else:
            country_code = input_data.get('install_country_code', '').replace('\n', ' ').replace('"', '')


    #This checks the dict key 'install_zip' and if the key does not exist it enters '' in as the value
    #If this statement is true it will use the dict key 'loc_zip'
    if input_data.get('install_zip', '') == '':

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'loc_zip'
        if input_data.get('loc_zip', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            loc_zip = input_data.get('loc_zip', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'loc_zip'
        else:
            loc_zip = input_data.get('loc_zip', '').replace('\n', ' ').replace('"', '')

    #If the statement is false it will use the dict key 'install_country_code'
    else:

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'loc_zip'
        if input_data.get('install_zip', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            loc_zip = input_data.get('install_zip', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'loc_zip'
        else:
            loc_zip = input_data.get('install_zip', '').replace('\n', ' ').replace('"', '')


    #This sets the variable 'location_name' to me 'company_name - location_city'
    location_name = company_name + ' - ' + location_city + ', ' + location_state


    #print('Customer Company Name: \'' + company_name + '\'')

    #print('Customer Location Name: \'' + location_name + '\'')

    #print('Customer First Name: \'' + name_first + '\'')

    #print('Customer Last Name: \'' + name_last + '\'')

    #print('Customer Email: \'' + alias + '\'')

    #print('Customer Phone Number: \'' + phone_number + '\'')

    #print('Customer Address: \'' + location_address + '\'')

    #print('Customer City: \'' + location_city + '\'')

    #print('Customer State: \'' + location_state + '\'')

    #print('Customer Country: \'' + country_code + '\'')

    #print('Customer Zipcode: \'' + loc_zip + '\'')

    #print('Customer Internal ID #: \'' + customer_id + '\'')

    #print('Variables Loaded')

    #print(' ')

    #print('Defining Company Function...')

    #This defines the function to make a new company in Snipe-IT.
    def new_company(company_name):

        #This next section breaks the str 'payload' apart, so that the varible 'company_name' can be used.
        payload = '{"name":"company_name"}'

        #This grabs {"name":"
        pay1 = payload[:9]

        #This grabs "}
        pay2 = payload[21:]

        #This combines the string back together but allows the use of the variable company_name.
        pay3 = pay1 + company_name + pay2

        url = "url"

        headers = {'content-type': 'application/json', 'Authorization': snipe_it_api_key}

        response = requests.post(url, data=pay3, headers=headers)

        #print(response.text)

    #print('Function Defined')

    #print(' ')

    #print('Creating Company...')

    #This runs the function 'new_company()' with the variable 'company_name'
    new_company(company_name)

    #print('Company Created.')

    #print(' ')

    #print('Defining company ID search function...')

    #This defines the function to search for companies by name.
    def search_company(company_name):

        url = "url"

        headers = {'Authorization': snipe_it_api_key, 'accept': 'application/json'}

        params = (
            ('search', company_name),
        )

        response = requests.request("GET", url, headers=headers, params=params)

        #This turns response.text into a .json file.
        response_json = json.loads(response.text)

        #This grabs the rows section from the response.
        response_list = response_json['rows']

        #This grabs the first item in the rows section.
        response_dict = response_list[0]

        #This grabs the company ID that we need for creating a new user.
        response_int = response_dict['id']

        #print('Company ID: ' + str(response_int))

        return(response_int)

    #print('Function Defined')

    #print(' ')

    #print('Searching for company ID...')

    #This runs the function 'search_company()' with the variable 'company_name' and saves the returned value to be the variable 'id_company' to be used later.
    id_company = search_company(company_name)

    #print('Company ID Found')

    #print(' ')

    #print('Defining New User function...')

    #This defines the function to add a new user in Snipe-IT.
    def new_user(name_first, name_last, alias, phone_number, id_company):

        url = "url"

        #This next section breaks the str 'payload' apart.
        payload = '{"first_name":"name_first","last_name":"name_last","username":"alias","password":"password","phone":"phone_number","company_id":id_company}'

        #This grabs {"first_name":" 
        pay4 = payload[:15]

        #This grabs ","last_name":" 
        pay5 = payload[25:40]

        #This grabs ","username":" 
        pay6 = payload[49:63]

        #This grabs ","password":"password","phone":" 
        pay7 = payload[68:123]

        #This grabs ","company_id": 
        pay8 = payload[135:150]

        #This grabs } 
        pay9 = payload[160:]

        #This combines the string back together but allows the use of the variables name_first, name_last, alias, phone_number, id_company, and customer_id.
        pay10 = pay4 + name_first + pay5 + name_last + pay6 + alias + pay7 + phone_number + pay8 + str(id_company) + ',"employee_num":"' + customer_id + '"' + pay9

        headers = {'content-type': 'application/json', 'Authorization': snipe_it_api_key}

        response = requests.request("POST", url, data=pay10, headers=headers)

        #print(response.text)

    #print('Function defined')

    #print(' ')

    #print('Creating New User...')

    #This runs the function 'new_user()' with the variables 'name_first', 'name_last', 'alias', 'phone_number', and 'id_company'.
    new_user(name_first, name_last, alias, phone_number, id_company)

    #print('New User created')

    #print(' ')

    #print('Defining Search for user ID function...')

    #This defines the function to search for users by username.
    def search_user(user_name):

        url = "url"

        headers = {'Authorization': snipe_it_api_key, 'accept': 'application/json'}

        params = (
            ('search', user_name),
        )

        response = requests.request("GET", url, headers=headers, params=params)

        #This turns response.text into a .json file.
        response_json = json.loads(response.text)

        #This grabs the rows section from the response.
        response_list = response_json['rows']

        #This grabs the first item in the rows section.
        response_dict = response_list[0]

        #This grabs the user ID that we need for creating a new location.
        response_int = response_dict['id']

        #print('User ID: ' + str(response_int))

        return(response_int)

    #print('Function defined')

    #print(' ')

    #print('Searching for user ID...')

    #This runs the function 'search_user()' with the variable 'alias' and saves the returned value to be the variable 'id_manager' to be used later.
    id_manager = search_user(alias)

    #print('User ID found')

    #print(' ')

    #print('Defining new Location function...')

    #This defines the function to add a new location in Snipe-IT.
    def new_location(location_name, location_address, location_city, location_state, country_code, loc_zip, id_manager):

        url = "url"

        payload = {"name": location_name, "address": location_address, "city": location_city, "state": location_state, "country": country_code, "zip": loc_zip, "manager_id": id_manager}

        #This url encodes payload. 
        encoded = urllib.parse.urlencode(payload)

        #This changes any + in encoded to %20
        encoded.replace( '+' , "%20")

        headers = {'Accept': "application/json", 'content-type': 'application/x-www-form-urlencoded', 'Cache-Control': "no-cache", 'Authorization': snipe_it_api_key}

        response = requests.request("POST", url, data=payload, headers=headers)

        #print(response.text)

    #print('Function Defined')

    #print(' ')

    #print('Creating New Location...')

    #This runs the function 'new_location()' with the variables 'location_name', 'location_address', 'location_city', 'location_state', 'country_code', 'loc_zip', and 'id_manager'.
    new_location(location_name, location_address, location_city, location_state, country_code, loc_zip, id_manager)

    #print('Location created')

    #print(' ')

    #print('#####PROCESS COMPLETE#####')

    #print(' ')

python python-3.x zapier
1个回答
1
投票

@Stael注意到我有两个返回,从运行一个烧瓶应用程序遗留下来,在功能之外列出。我删除了那些,代码按预期工作,没有来自Zapier的错误。

回答2/8:

import requests  
import json  
import urllib  

if input_data.get('upgrade_response', '') == 'No change necessary, continue with the current hardware and agreement.':  
    pass  
##########edited line above from "return '', 200" to "pass"##########

else:  
    #print(request.json)  

    #print('Loading Variables...')  

    #This is the API key for the user in Snipe-IT  
    snipe_it_api_key = 'api_key'  

    #This checks the dict key 'customer_name' and if the key does not exist it enters '. .' in as the value  
    #This checks to see if the last character in the value is ' '  
    #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'customer_name'  
    if input_data.get('customer_name', '. .').replace('\n', ' ').replace('"', '')[-1] == ' ':  
        customer_name = input_data.get('customer_name', '. .').replace('\n', ' ').replace('"', '')[:-1]  

    #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'customer_name'  
    else:  
        customer_name = input_data.get('customer_name', '. .').replace('\n', ' ').replace('"', '')  


    #This splits the variable 'customer_name' into a list at any ' '  
    customer_name_split = customer_name.split()  

    #This grabs the first item in the list and saves it as the variable 'customer_name_first'  
    customer_name_first = customer_name_split[0]  

    #This grabs the last item in the list and saves it as the variable 'customer_name_last'  
    customer_name_last = customer_name_split[-1]  


    #This checks the dict key 'customer_name' and if the key does not exist it enters '' in as the value  
    #This checks to see if the last character in the value is ' '  
    #If this statement is true, it goes through and removes the last character at the end of the value then   
    #it checks for and removes any "’" "'" or '"' that are found in the value and it checks for and replaces any '\n' with ' ' that are found in the value then saves it as the variable 'company_name'  
    if input_data.get('company_name', '').replace('\n', ' ').replace('"', '')[-1] == ' ':  
        company_name = input_data.get('company_name', '').replace('\n', ' ').replace('"', '').replace("'", '').replace("`", '').replace("’", '')[:-1]  

    #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'company_name'
    else:
        company_name = input_data.get('company_name', '').replace('\n', ' ').replace('"', '').replace("'", '').replace("`", '').replace("’", '')


    #This checks the dict key 'name_first' and if the key does not exist it enters '' in as the value
    #This checks to see if the last character in the value is ' '
    #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'name_first'
    if (input_data.get('name_first', '').replace('\n', ' ').replace('"', '') + customer_name_first.replace('.', ''))[-1] == ' ':
        name_first = (input_data.get('name_first', '').replace('\n', ' ').replace('"', '') + customer_name_first.replace('.', ''))[:-1]

    #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'name_first'
    else:
        name_first = input_data.get('name_first', '').replace('\n', ' ').replace('"', '') + customer_name_first.replace('.', '')


    #This checks the dict key 'name_last' and if the key does not exist it enters '' in as the value
    #This checks to see if the last character in the value is ' '
    #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'name_last'
    if (input_data.get('name_last', '').replace('\n', ' ').replace('"', '') + customer_name_last.replace('.', ''))[-1] == ' ':
        name_last = (input_data.get('name_last', '').replace('\n', ' ').replace('"', '') + customer_name_last.replace('.', ''))[:-1]

    #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'name_last'
    else:
        name_last = input_data.get('name_last', '').replace('\n', ' ').replace('"', '') + customer_name_last.replace('.', '')


    #This checks the dict key 'alias' and if the key does not exist it enters '' in as the value
    #This checks to see if the last character in the value is ' '
    #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'alias'
    if input_data.get('alias', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
        alias = input_data.get('alias', '').replace('\n', ' ').replace('"', '')[:-1]

    #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'alias'
    else:
        alias = input_data.get('alias', '').replace('\n', ' ').replace('"', '')


    #This checks the dict key 'customer_id' and if the key does not exist it enters '' in as the value
    #This checks to see if the last character in the value is ' '
    #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'customer_id'
    if input_data.get('customer_id', ' ').replace('\n', ' ').replace('"', '')[-1] == ' ':
        customer_id = input_data.get('customer_id', '').replace('\n', ' ').replace('"', '')[:-1]

    #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'customer_id'
    else:
        customer_id = input_data.get('customer_id', '').replace('\n', ' ').replace('"', '')


    #This checks the dict key 'phone_number' and if the key does not exist it enters '' in as the value
    #This checks to see if the last character in the value is ' '
    #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' or '-' or '(' or ')' that are found in the value then saves it as the variable 'phone_number'
    if input_data.get('phone_number', '').replace('\n', ' ').replace('"', '').replace('-', '').replace('(', '').replace(')', '')[-1] == ' ':
        phone_number = input_data.get('phone_number', '').replace('\n', ' ').replace('"', '').replace('-', '').replace('(', '').replace(')', '')[:-1]

    #If the statement is false, it checks for and changes any '\n' or '"' or '-' or '(' or ')' that are found in the value then saves it as the variable 'phone_number'
    else:
        phone_number = input_data.get('phone_number', '').replace('\n', ' ').replace('"', '').replace('-', '').replace('(', '').replace(')', '')


    #This checks the dict key 'install_address' and if the key does not exist it enters '' in as the value
    #If this statement is true it will use the dict key 'location_address'
    if input_data.get('install_address', '') == '':

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_address'
        if input_data.get('location_address', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            location_address = input_data.get('location_address', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_address'
        else:
            location_address = input_data.get('location_address', '').replace('\n', ' ').replace('"', '')

    #If the statement is false it will use the dict key 'install_address'
    else:

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_address'
        if input_data.get('install_address', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            location_address = input_data.get('install_address', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_address'
        else:
            location_address = input_data.get('install_address', '').replace('\n', ' ').replace('"', '')


    #This checks the dict key 'install_city' and if the key does not exist it enters '' in as the value
    #If this statement is true it will use the dict key 'location_city'
    if input_data.get('install_city', '') == '':

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_city'
        if input_data.get('location_city', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            location_city = input_data.get('location_city', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_city'
        else:
            location_city = input_data.get('location_city', '').replace('\n', ' ').replace('"', '')

    #If the statement is false it will use the dict key 'install_city'
    else:

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_city'
        if input_data.get('install_city', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            location_city = input_data.get('install_city', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_city'
        else:
            location_city = input_data.get('install_city', '').replace('\n', ' ').replace('"', '')


    #This checks the dict key 'install_state' and if the key does not exist it enters '' in as the value
    #If this statement is true it will use the dict key 'location_state'
    if input_data.get('install_state', '') == '':

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_state'
        if input_data.get('location_state', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            location_state = input_data.get('location_state', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_state'
        else:
            location_state = input_data.get('location_state', '').replace('\n', ' ').replace('"', '')

    #If the statement is false it will use the dict key 'install_state'
    else:

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_state'
        if input_data.get('install_state', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            location_state = input_data.get('install_state', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'location_state'
        else:
            location_state = input_data.get('install_state', '').replace('\n', ' ').replace('"', '')


    #This checks the dict key 'install_country_code' and if the key does not exist it enters '' in as the value
    #If this statement is true it will use the dict key 'country_code'
    if input_data.get('install_country_code', '') == '':

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'country_code'
        if input_data.get('country_code', ' ').replace('\n', ' ').replace('"', '')[-1] == ' ':
            country_code = input_data.get('country_code', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'country_code'
        else:
            country_code = input_data.get('country_code', '').replace('\n', ' ').replace('"', '')

    #If the statement is false it will use the dict key 'install_country_code'
    else:

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'country_code'
        if input_data.get('install_country_code', '') == '':
            country_code = input_data.get('install_country_code', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'country_code'
        else:
            country_code = input_data.get('install_country_code', '').replace('\n', ' ').replace('"', '')


    #This checks the dict key 'install_zip' and if the key does not exist it enters '' in as the value
    #If this statement is true it will use the dict key 'loc_zip'
    if input_data.get('install_zip', '') == '':

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'loc_zip'
        if input_data.get('loc_zip', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            loc_zip = input_data.get('loc_zip', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'loc_zip'
        else:
            loc_zip = input_data.get('loc_zip', '').replace('\n', ' ').replace('"', '')

    #If the statement is false it will use the dict key 'install_country_code'
    else:

        #This checks to see if the last character in the value is ' '
        #If this statement is true, it goes through and removes the last character at the end of the value then it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'loc_zip'
        if input_data.get('install_zip', '').replace('\n', ' ').replace('"', '')[-1] == ' ':
            loc_zip = input_data.get('install_zip', '').replace('\n', ' ').replace('"', '')[:-1]

        #If the statement is false, it checks for and changes any '\n' or '"' that are found in the value then saves it as the variable 'loc_zip'
        else:
            loc_zip = input_data.get('install_zip', '').replace('\n', ' ').replace('"', '')


    #This sets the variable 'location_name' to me 'company_name - location_city'
    location_name = company_name + ' - ' + location_city + ', ' + location_state


    #print('Customer Company Name: \'' + company_name + '\'')

    #print('Customer Location Name: \'' + location_name + '\'')

    #print('Customer First Name: \'' + name_first + '\'')

    #print('Customer Last Name: \'' + name_last + '\'')

    #print('Customer Email: \'' + alias + '\'')

    #print('Customer Phone Number: \'' + phone_number + '\'')

    #print('Customer Address: \'' + location_address + '\'')

    #print('Customer City: \'' + location_city + '\'')

    #print('Customer State: \'' + location_state + '\'')

    #print('Customer Country: \'' + country_code + '\'')

    #print('Customer Zipcode: \'' + loc_zip + '\'')

    #print('Customer Internal ID #: \'' + customer_id + '\'')

    #print('Variables Loaded')

    #print(' ')

    #print('Defining Company Function...')

    #This defines the function to make a new company in Snipe-IT.
    def new_company(company_name):

        #This next section breaks the str 'payload' apart, so that the varible 'company_name' can be used.
        payload = '{"name":"company_name"}'

        #This grabs {"name":"
        pay1 = payload[:9]

        #This grabs "}
        pay2 = payload[21:]

        #This combines the string back together but allows the use of the variable company_name.
        pay3 = pay1 + company_name + pay2

        url = "url"

        headers = {'content-type': 'application/json', 'Authorization': snipe_it_api_key}

        response = requests.post(url, data=pay3, headers=headers)

        #print(response.text)

    #print('Function Defined')

    #print(' ')

    #print('Creating Company...')

    #This runs the function 'new_company()' with the variable 'company_name'
    new_company(company_name)

    #print('Company Created.')

    #print(' ')

    #print('Defining company ID search function...')

    #This defines the function to search for companies by name.
    def search_company(company_name):

        url = "url"

        headers = {'Authorization': snipe_it_api_key, 'accept': 'application/json'}

        params = (
            ('search', company_name),
        )

        response = requests.request("GET", url, headers=headers, params=params)

        #This turns response.text into a .json file.
        response_json = json.loads(response.text)

        #This grabs the rows section from the response.
        response_list = response_json['rows']

        #This grabs the first item in the rows section.
        response_dict = response_list[0]

        #This grabs the company ID that we need for creating a new user.
        response_int = response_dict['id']

        #print('Company ID: ' + str(response_int))

        return(response_int)

    #print('Function Defined')

    #print(' ')

    #print('Searching for company ID...')

    #This runs the function 'search_company()' with the variable 'company_name' and saves the returned value to be the variable 'id_company' to be used later.
    id_company = search_company(company_name)

    #print('Company ID Found')

    #print(' ')

    #print('Defining New User function...')

    #This defines the function to add a new user in Snipe-IT.
    def new_user(name_first, name_last, alias, phone_number, id_company):

        url = "url"

        #This next section breaks the str 'payload' apart.
        payload = '{"first_name":"name_first","last_name":"name_last","username":"alias","password":"password","phone":"phone_number","company_id":id_company}'

        #This grabs {"first_name":" 
        pay4 = payload[:15]

        #This grabs ","last_name":" 
        pay5 = payload[25:40]

        #This grabs ","username":" 
        pay6 = payload[49:63]

        #This grabs ","password":"password","phone":" 
        pay7 = payload[68:123]

        #This grabs ","company_id": 
        pay8 = payload[135:150]

        #This grabs } 
        pay9 = payload[160:]

        #This combines the string back together but allows the use of the variables name_first, name_last, alias, phone_number, id_company, and customer_id.
        pay10 = pay4 + name_first + pay5 + name_last + pay6 + alias + pay7 + phone_number + pay8 + str(id_company) + ',"employee_num":"' + customer_id + '"' + pay9

        headers = {'content-type': 'application/json', 'Authorization': snipe_it_api_key}

        response = requests.request("POST", url, data=pay10, headers=headers)

        #print(response.text)

    #print('Function defined')

    #print(' ')

    #print('Creating New User...')

    #This runs the function 'new_user()' with the variables 'name_first', 'name_last', 'alias', 'phone_number', and 'id_company'.
    new_user(name_first, name_last, alias, phone_number, id_company)

    #print('New User created')

    #print(' ')

    #print('Defining Search for user ID function...')

    #This defines the function to search for users by username.
    def search_user(user_name):

        url = "url"

        headers = {'Authorization': snipe_it_api_key, 'accept': 'application/json'}

        params = (
            ('search', user_name),
        )

        response = requests.request("GET", url, headers=headers, params=params)

        #This turns response.text into a .json file.
        response_json = json.loads(response.text)

        #This grabs the rows section from the response.
        response_list = response_json['rows']

        #This grabs the first item in the rows section.
        response_dict = response_list[0]

        #This grabs the user ID that we need for creating a new location.
        response_int = response_dict['id']

        #print('User ID: ' + str(response_int))

        return(response_int)

    #print('Function defined')

    #print(' ')

    #print('Searching for user ID...')

    #This runs the function 'search_user()' with the variable 'alias' and saves the returned value to be the variable 'id_manager' to be used later.
    id_manager = search_user(alias)

    #print('User ID found')

    #print(' ')

    #print('Defining new Location function...')

    #This defines the function to add a new location in Snipe-IT.
    def new_location(location_name, location_address, location_city, location_state, country_code, loc_zip, id_manager):

        url = "url"

        payload = {"name": location_name, "address": location_address, "city": location_city, "state": location_state, "country": country_code, "zip": loc_zip, "manager_id": id_manager}

        #This url encodes payload. 
        encoded = urllib.parse.urlencode(payload)

        #This changes any + in encoded to %20
        encoded.replace( '+' , "%20")

        headers = {'Accept': "application/json", 'content-type': 'application/x-www-form-urlencoded', 'Cache-Control': "no-cache", 'Authorization': snipe_it_api_key}

        response = requests.request("POST", url, data=payload, headers=headers)

        #print(response.text)

    #print('Function Defined')

    #print(' ')

    #print('Creating New Location...')

    #This runs the function 'new_location()' with the variables 'location_name', 'location_address', 'location_city', 'location_state', 'country_code', 'loc_zip', and 'id_manager'.
    new_location(location_name, location_address, location_city, location_state, country_code, loc_zip, id_manager)

    #print('Location created')

    #print(' ')

    #print('#####PROCESS COMPLETE#####')

    #print(' ')

##########removed "return '', 200" on this line from the code##########

© www.soinside.com 2019 - 2024. All rights reserved.