Building a py with multiple parameters for API calls.

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Tourless
Pro Scripter
Posts: 69
Joined: Wed Jun 14, 2017 1:53 am
Location: NY

Building a py with multiple parameters for API calls.

Post by Tourless » Fri Oct 05, 2018 5:38 pm

Hi Folks,
I'll start by saying I've never worked with an API before and I'm sure there are more advanced ways of handling my problem but I myself and not that advanced. That being said I'm using MS to build the file framework but I need to account for multiple items to be processed in the same file. The py file I'm building looks like this, as created through my script...

Code: Select all

import httplib, urllib, base64

headers = {
    # Request headers
    'Authorization': '********',
    'EnvironmentCode': '********',
    'Ocp-Apim-Subscription-Key': '********',
}

params = urllib.urlencode({
})

try:
    conn = httplib.HTTPSConnection('***.**-api.net')
    conn = httplib.HTTPSConnection('***.**-api.net')
    conn.request("DELETE", "/admin/location/46208?%s" % params, "{body}", headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
except Exception as e:
    print("Errno {0}] {1}".format(e,errno, e.strerror))
'conn.request("DELETE", "/admin/location/46208' - this is the id upon which the delete action is to take place.

Now I have an excel list of about 1500 id's so my thought is repeat the try statement and exception part of my code, using the id as a variable being read from the excel file for each iteration. The only problem is I don't know how to do this :P

Can anyone point me in the right direction while I'm banging my head against the wall trying to figure it out for myself? Like, there has to be a way to create a reusable code block or perhaps read from a text file, cut and paste the try statement up until the id, then fill the id with the current value form the current iteration on the excel file, then continue a copy and paste for the remainder of the code. Then repeat that process for however many instances are counted from the excel file. Right? That sounds feasible. Right?

UPDATE 1:
I've split my generated code into three text files, header, try1, and try2, and I'm reading my excel sheet into an array. Now to see if I can figure out the number of elements in my array then structure a loop accordingly... I think.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Building a py with multiple parameters for API calls.

Post by Marcus Tettmar » Sat Oct 06, 2018 11:23 am

Not wanting to "pass the buck" but this looks like a request for help with Python rather than anything to do with Macro Scheduler, and, apart from Python not being our remit, it isn't our strength so I'd recommend you post this in a Python forum instead.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Tourless
Pro Scripter
Posts: 69
Joined: Wed Jun 14, 2017 1:53 am
Location: NY

Re: Building a py with multiple parameters for API calls.

Post by Tourless » Mon Oct 08, 2018 5:18 pm

I've got the python part covered, what I need to learn in MS is reading and copying array values to a file of my choosing.
Update:
Here's my solution...

Code: Select all

CapsOff
Label>HEADER
ReadFile>C:\py\header.txt,header
PutClipBoard>header
Run>Notepad.exe
WaitWindowOpen>Untitled - Notepad
SetFocus>Untitled - Notepad
Press CTRL
Send>v
Release CTRL


//Specify the path of the Excel file here
Let>filename=C:\py\DeleteLandmarkIDs.xls
IfFileExists>filename
  XLOpen>filename,1,xlH
  XLGetSheetDims>xlH,Sheet1,rows,cols
  Let>totalrows=rows
   Let>row=0
   Repeat>row
   Label>TRY1
   ReadFile>C:\py\try1.txt,try1
   PutClipBoard>try1
   //Wait>.1
   SetFocus>Untitled - Notepad
   Press CTRL
   Send>v
   Release CTRL
   
   Label>Insert Landmark ID
   Let>row=row+1
   //get the fields for this row
   XLGetCell>xlH,Sheet1,row,1,data
   //trim the results (Excel sometimes adds CRLFs)
   Trim>data,data
   //For this example we'll just paste each row into Notepad
   SetFocus>Notepad*
   Send>%data%
   //Wait>0.1

   Label>TRY2
   ReadFile>C:\py\try2.txt,try2
   PutClipBoard>try2
   Wait>.25
   SetFocus>Untitled - Notepad
   Press CTRL
   Send>v
   Release CTRL
   Press Enter * 2
   Until>row>totalrows
Else
Endif
messagemodal>done!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts