Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python classes
#1
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
Reply
#2
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.
Reply
#3
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?
Reply
#4
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...
Reply
#5
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
Reply
#6
import requests

urls = [
    "first",
    "second"
]

for url in urls:
    requests.post("/uri/{0}".format(url))
Reply
#7
How to do it if the arguments are listed in a text file?
Reply
#8
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
(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.
Reply
#10
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/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Classes rob101 4 527 Feb-05-2024, 06:51 PM
Last Post: rob101
  Understanding Python classes PythonNewbee 3 1,184 Nov-10-2022, 11:07 PM
Last Post: deanhystad
Sad Python classes PythonNewbee 4 1,047 Nov-09-2022, 01:19 PM
Last Post: deanhystad
  Inheritance vs Instantiation for Python classes mr_byte31 7 2,853 Oct-14-2021, 12:58 PM
Last Post: mr_byte31
  Understanding Python super() for classes OmegaRed94 1 1,832 Jun-09-2021, 09:02 AM
Last Post: buran
  Python classes Python_User 15 4,862 Aug-04-2020, 06:57 PM
Last Post: Python_User
  Python Classes leodavinci1990 1 2,083 Nov-27-2019, 07:25 AM
Last Post: buran
  Using classes? Can I just use classes to structure code? muteboy 5 5,037 Nov-01-2017, 04:20 PM
Last Post: metulburr
  use of classes in python Lux 2 3,511 Aug-19-2017, 12:29 PM
Last Post: hbknjr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020