I want to start writing scripts so that I can interact using APIs with some vendor websites.
Are there any online resources that can explain how to start this?
Also, how do i know what classes or functions are part of a module that i have imported. For example if import json module how do i know all the classes and function in it?
my code here
You sound like pretty new to Python/programming as whole.
You can check our Tutorials section. There is also dedicated Free python resources thread.
https://python-forum.io/Forum-Tutorials
Google is always of help
https://www.google.bg/search?q=python+api+tutorial
Finding the rihgt class/resource for you will depend a lot on your previous experience/knowledge
Many APIs have python documentation/examples/wrappers - provided/endorsed by the service (official) or unofficial one, by third-party
e.g. here is a list (in-exhaustive) of python wrappers for different APIs:
https://github.com/realpython/list-of-py...i-wrappers
As to your second question - start by reading/browsing the docs for the particular package/module. It maybe good or bad, but is always a good starting point. And it's always good to have the documentation at hand for reference later on. Python is good because well-written code is self-documenting (e.g. using doc-strings).
If documentation is not good/insufficient you can always browse the code itself.
Thanks Buran, I found these sites before, but none of them properly explains how to write scripts to achieve basic tasks like capturing a authentication token, using it to make other api calls etc.,
Can you help me in this regard please?
let's narrow your question, because at the moment it is very broad/generic...
Which API do you want to use? As I said most APIs would provide documentation. This is especially true when it comes to APIs with more complex authentication protocol. Let's start from there, see if there is wrapper and what the authentication protocol is...
Thanks for the help Buran. I got it working with a few hints from here and there.
Here is a bash script i use often
for i in cat list
;do POST /uri/$i;done
list contains a list if parameter.
What would the equivalent of this be in python?
Essentially iam trying to send a arguments from a list to a POST request in an API call
import requests
urls = [
"first",
"second"
]
for url in urls:
requests.post("/uri/{0}".format(url))
How to do it if the arguments are listed in a text file?
First you should
import this
Then you should pick a random module from the Python stdlib and import it:
import json
To get an overview of the json module, you should type
help(json)
. Then you'll see the docstrings and the content of the module. It's very helpful to use the Python
repl
.
(Aug-11-2017, 04:05 AM)prsdll6 Wrote: [ -> ]How to do it if the arguments are listed in a text file?
Open the file and read from it.
I am using the following:
filename = "zonelist"
file = open(filename, "r")
for line in zonlist:
response, content = http.request('https://api-example.com/rest/addns/ + line', 'POST', json.JSONEncoder().encode(array), headers={'Content-type': 'application/json', 'Auth-Token': token})
where zonelist has the following in it:
testing1.com/
testing2.com/
testing3.com/
for some reason, i keep getting the zone not found error. not sure if string concatenation is working correct here or not :
'https://api.example.com/rest/addns/ + line'
the url in the end should look like -
https://api-example.com/rest/addns/testing1.com/