Python Forum
API REST Package for Calling/Flask
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
API REST Package for Calling/Flask
#1
Greetings,

Is there an API package for calling REST api's? I looked into FastAPI but that is for creating APIs.

Can this be done with Flask?

I would want to rewrite the URLs based on what I want. Let's say I want to consume a Movie API. The URLs would look like this:

http://www.mydomain.com/movie/ID ID would be a place holder for a the movie id. Then I would click the link and view the details of the movie, etc...

I would have classes for each end point but I would not store the data in a database. Just send a partial request and and based on the Etag (I believe), I would request or do nothing.

Classes:
Titles = Movie or TV
Person
Category
Roles
etc...

I know I can use the 'requests' package but if I would need to use a web framework anyway if I wish to build a web site.

Maybe Flask has a package for APIs? I know Flask has a something close to the MVC convention. However, I looked over the docs and it seems as though the app.py is similar to a controller in PHP frameworks. However, I don't like the idea of having all of my functions in one file.

Can you split up the directories: Movie > movie.py (same as app.py but less code), etc...?



Thanks,
Reply
#2
I'm confused what you're asking. You say you want to make a call to a REST API, but you seem to think using, say, Requests is unsuitable. Why is that? It's an HTTP client, which is what you need to call what people talk about when they say REST API.
Reply
#3
I understand that. So, basically - if I were to use Flask, I would use the Requests package. But I also had some other questions regarding Flask, which included questions about breaking up the app.py file into smaller more specific files and if URL rewriting is possible with Flask?

(Oct-16-2021, 06:43 PM)ndc85430 Wrote: I'm confused what you're asking. You say you want to make a call to a REST API, but you seem to think using, say, Requests is unsuitable. Why is that? It's an HTTP client, which is what you need to call what people talk about when they say REST API.
Reply
#4
(Oct-16-2021, 06:39 PM)muzikman Wrote: Maybe Flask has a package for APIs? I know Flask has a something close to the MVC convention. However, I looked over the docs and it seems as though the app.py is similar to a controller in PHP frameworks. However, I don't like the idea of having all of my functions in one file.
There are serval eg Flask-REST-JSONAPI, Flask-RESTPlus.
A tutorial How to build a JSON API with Python

Quote:However, I don't like the idea of having all of my functions in one file
It's normal for any lager project to spilt up it in serval files eg views, model...ect,
in Flask you have full freedom(good and bad).
What i can mean bye bad is eg Django you get full project in a MVC style,in Flask you have to construct this yourself.
I use similar setup as this Organizing a Flask Project Beyond Single File
There are a lot tutorial for this in Flask and cookiecutter if search for Flask(cookiecutter) there are many project starter templates.
Reply
#5
I am not interested in building an API but consuming one. I want to work on this project for part of my portfolio. I already know (always learning) HTML 5, CSS3, SASSS, responsive design (not using bootstrap), SQL and currently learning Modern Javascript so it's the next normal step. I started to learn Python as a general language but my heart is in the web. I don't have any interest in building GUI apps; maybe some command line in Linux but the web is what I already have experience in.

I started in 1998 with Cold Fusion and an Access Database. Then, just classic ASP, ASP.net with VB, then C#, MS SQL server, then on to the open source market.

I want to build a site using the IMDB API and another for music, probably MusicBrainz



It's only been six months since I started my Python journey. Modern Javascript 2021 has incorporated a lot of Python features, generators, *args and a ton more. I am still waiting for the day when Python adds interfaces, real private and protect keywords. I am sure they will get to this.

Questions
====================================

  1. Do you think I should just learn Django and skip Flask?


  2. Is Django MVC or MVT (forget the acronym) like Flask?


  3. Is there a big demand for Python web developers? These days, everyone is using these turnkey solutions or CMS.
Reply
#6
(Oct-18-2021, 12:46 PM)muzikman Wrote: I am not interested in building an API but consuming one.
Then just use Requests
(Oct-18-2021, 12:46 PM)muzikman Wrote: It's only been six months since I started my Python journey. Modern Javascript 2021 has incorporated a lot of Python features, generators, *args and a ton more.
Sure Modern JavaScript have gotten better and now quite similar to Python,
writing some when make a website in Flask or Django is not that difficult.
Quote: I am still waiting for the day when Python adds interfaces, real private and protect keywords. I am sure they will get to this
For sure don't need that Python,there is other way(better) to solve this in Python.
We are all consenting adults

Quote:Do you think I should just learn Django and skip Flask?
Up to you look into both.

Quote:Is Django MVC or MVT (forget the acronym) like Flask?
Dos it really matter?
The Django devs just use different names for the view (template) and the controller (view),
but in essence they are the same thing as in any other MVC framework.

Flask is neither,it's all up to you how to structure it in Flask,i use have MVC like structure style for larger project as mention in last post.
Quote:Is there a big demand for Python web developers? These days, everyone is using these turnkey solutions or CMS.
Python is now one the most popular language so there is demand on web side to,i don't follow the Job market so you have to search for that.
Reply
#7
Thank you for answering my questions.

One thing that confuses me. I see some developers using a single underscore for private and others use a double underscore for private. When I write classes with inheritance; the single underscore has proven to be protected. I tested it myself.

What are your thoughts on this?

Thanks,
Reply
#8
(Oct-19-2021, 03:39 PM)muzikman Wrote: One thing that confuses me. I see some developers using a single underscore for private and others use a double underscore for private. When I write classes with inheritance; the single underscore has proven to be protected. I tested it myself.
The single underscore is a advice that it best to leave it alone,it don't protect anything as that's not a thing in Python.
Double underscore is really meant for name mangling not for private use.
class Test:
    # car_id is not meant to be a part of the public interface
    # So it's a advice that is best to leave it alone
    _car_id = 12345

    def __init__(self):
        self.car_color = 'Blue'
Still available if call it.
>>> car =  Test()
>>> car.car_color
'Blue'
>>> car._car_number
12345
class Foo:
    def __init__(self):
        # Not meant as private(can keep private copy when name mangling occur)
        self.__var = 3 
 
class Bar(Foo):
    def __init__(self):
        super().__init__()
        self.__var = 3
>>> obj = Bar()
>>> # Both have been given different name(name mangling)
>>> obj._Foo__var
3
>>> obj._Bar__var
3
>>> 
>>> # Change one will not change the other
>>> obj._Foo__var = 100
>>> obj._Foo__var
100
>>> obj._Bar__var
3
Python's Class Development Toolkit at this time he explain usage of double underscore.
Reply
#9
Yes, I have seen these videos. This guy is great. So, when designing a class, what things make you consider using name mangling? I am thinking that it mangles the name in order to prevent name collision, maybe? We can still access the variable even after we mangle it?

I've never heard of the __slots__ dunder. When should it be used?


You answer almost all of the questions on this site. You must be a Python master.
Reply
#10
(Oct-20-2021, 01:08 PM)muzikman Wrote: This guy is great. So, when designing a class, what things make you consider using name mangling? I am thinking that it mangles the name in order to prevent name collision, maybe? We can still access the variable even after we mangle it?
If look at my last example so do sub-class Bar use same name __var = 3,normally would this override the firs one.
Name mangling is helpful for letting subclasses override methods without breaking intraclass method calls.
Let say a new company say a car is newer than is bye override method year_made.
I have not find to much usage for this yet.
class CarMaker:
    # car_id is not meant to be a part of the public interface
    # So it's a advice that is best to leave it alone
    _car_id = 12345

    def __init__(self, color):
        self.car_color = color

    def year_made(self):
        return 2019

    __year_made = year_made # Private copy of original year_made() method

class NewCompany(CarMaker):
    def year_made(self):
        return 2020
Use:
>>> car = NewCompany('Red')
>>> car.year_made()
2020
>>> # Carmaker still have orginal year made
>>> car._CarMaker__year_made()
2019
(Oct-20-2021, 01:08 PM)muzikman Wrote: I've never heard of the __slots__ dunder. When should it be used?
It's for memory saving and faster attribute access,look at link.

(Oct-20-2021, 01:08 PM)muzikman Wrote: You answer almost all of the questions on this site. You must be a Python master.
No it's just hobby that i used some(or lot time in periods) time over many years.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with return on REST API Flask korenron 0 1,286 Jun-13-2021, 10:40 AM
Last Post: korenron
  Calling Oracle REST SQL from Python johnjacob 2 1,994 Nov-05-2020, 04:19 AM
Last Post: johnjacob
  rest api parameter in flask bluefrog 3 3,242 Jun-21-2018, 05:03 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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