Created
November 3, 2019 21:23
-
-
Save jcmaslan/21c2b168bb3d46dedd8259052184f50a to your computer and use it in GitHub Desktop.
Set the event_auto_upload_query_filter for CSV file list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Example: | |
config_boxes.py --file test_config_boxes.csv --token MYTOKEN --dryrun | |
Loops through each row of CSV file and posts the new config for event_auto_upload_query_filter like: | |
{ | |
"device_id": "AQAG***REDACTED***MrOIwnr", | |
"event_auto_upload_query_filter": { | |
"text": "7pm to 10am" | |
} | |
} | |
""" | |
import pandas as pd | |
import argparse | |
import requests | |
class BoxConfigHandler(object): | |
""" | |
Loops run() loops through input CSV and posts new | |
event_auto_upload_query_filter text for each device_id. | |
""" | |
def __init__(self, args): | |
self.file = args['file'] | |
self.output = args['output'] | |
self.token = args['token'] | |
self.dryrun = args['dryrun'] | |
self.key_column = args['key_column'] | |
self.value_column = args['value_column'] | |
self.result_column = args['result_column'] | |
assert self.file is not None, "missing input csv file" | |
assert self.token is not None, "missing OAuth token" | |
assert self.key_column is not None, "missing key_column name that contains device_id" | |
assert self.value_column is not None, "missing value_column name with new target value for event_auto_upload_query_filter" | |
assert self.result_column is not None, "missing result_column name" | |
def run(self): | |
self.operate_on_file(self.file, outfile=self.output) | |
def post_query_filter(self, device_id, query_text): | |
""" returns the status_code and json payload of the POST """ | |
assert device_id is not None, "device_id required" | |
assert query_text is not None, "query_text required" | |
url = "https://camio.com/api/devices/requests/configuration" | |
query = {"text": query_text} | |
payload = {"device_id": device_id, "event_auto_upload_query_filter": query} | |
if self.dryrun: | |
return 200, payload | |
else: | |
r = requests.post(url, headers={"Authorization": "token "+self.token}, json=payload) | |
return r.status_code, payload | |
def operate_on_file(self, fname, outfile=None): | |
""" Get the file and operate on top of it """ | |
new_df = self.operate_on_df(pd.read_csv(fname)) | |
self.write_df_to_file(new_df, outfile) | |
def process_row(self, row): | |
""" Function to be applied on each row.""" | |
try: | |
device_id = row[self.key_column] | |
if row.isna()[self.value_column]: | |
query_text = '' #empty string to remove the query filter | |
else: | |
query_text = row[self.value_column] | |
return self.post_query_filter(device_id, query_text) | |
except KeyError: | |
return 400, {"message": "missing required columns device_id and event_auto_upload_query_filter_new"} | |
def operate_on_df(self, df): | |
""" Get the data frame and operate on top of it """ | |
assert isinstance(df, pd.DataFrame), "Data not in the form of Pandas DataFrame" | |
fn = self.process_row | |
new_column_name = self.result_column | |
result = df.apply(lambda x: fn(x), axis=1) | |
df[new_column_name] = result | |
print result | |
return df | |
def write_df_to_file(self, df, fname=None): | |
if fname is None: | |
fname = 'outfile.csv' | |
assert isinstance(df, pd.DataFrame), "Data not in the form of Pandas DataFrame" | |
df.to_csv(fname) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='CSV box config utility') | |
parser.add_argument('-t', '--token', type=str, default=None, | |
help="Your OAuth token used for the POST requests") | |
parser.add_argument('-f', '--file', type=str, default=None, | |
help="CSV file for import") | |
parser.add_argument('-o', '--output', type=str, default=None, | |
help="Output CSV file") | |
parser.add_argument('-k', '--key_column', type=str, default="device_id", | |
help="Name of column that contains the device_id (default is device_id)") | |
parser.add_argument('-v', '--value_column', type=str, default="event_auto_upload_query_filter_new", | |
help="Column name with the new target value for event_auto_upload_query_filter (default is event_auto_upload_query_filter_new)") | |
parser.add_argument('-r', '--result_column', type=str, default="post_result", | |
help="The new column name appended to contain result from POST (default post_result)") | |
parser.add_argument('-d', '--dryrun', action='store_true', | |
help="Show what will be done without doing it") | |
args = vars(parser.parse_args()) | |
if args['file'] is not None: | |
bch = BoxConfigHandler(args); | |
bch.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment