https://github.com/codeofrahul/flask_api
In this repository I have added the code of Flask api and tested the api using Postman.
https://github.com/codeofrahul/flask_api
api flask-api flask-restful postman-test python3
Last synced: 10 months ago
JSON representation
In this repository I have added the code of Flask api and tested the api using Postman.
- Host: GitHub
- URL: https://github.com/codeofrahul/flask_api
- Owner: CodeofRahul
- License: mit
- Created: 2024-12-22T08:51:49.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-24T08:01:33.000Z (about 1 year ago)
- Last Synced: 2025-01-30T03:18:59.726Z (about 1 year ago)
- Topics: api, flask-api, flask-restful, postman-test, python3
- Language: Python
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Flask_api
## Command Prompt (CMD) shortcuts:
```
- To create directory/Folder = `mkdir `
- To remove directory/Folder = `rmdir `
- To create file = `type nul > app.py`
- to create file using CMD/Powershell : `type (type template.py)`
```
## Venv shortcuts:
- To create environment = `conda create -p flask_env -y`
- To check available envs = `conda env list`
- To check available envs = `conda info --envs`
- To activate environment = `conda activate flask_env`
- To install requirements.txt = `pip install -r requirements.txt`
## Package related shortcuts
- To check install packages = `pip list`
- To check detailed about package = `pip show package_name`
- To install package = `pip install package_name`
- To uninstall package = `pip uninstall package_name`
- To check python installed version = `python --version`
## Git Command
- To add all file = `git add .`
- To add any particular file = `git add `
- To commit = `git commit -m "commit message"`
- To push the code = `git push origin main`
## most common HTTP methods
**GET** : The GET method is used to retrieve data on a server. Clients can use the GET method to access all of the resources of a given type, or they can use it to access a specific resource. For instance, a GET request to the `/products` endpoint of an e-commerce API would return all of the products in the database, while a GET request to the `/products/123` endpoint would return the specific product with an `ID` of `123`. GET requests typically do not include a request body, as the client is not attempting to create or update data.
**POST** : The POST method is used to create new resources. For instance, if the manager of an e-commerce store wanted to add a new product to the database, they would send a POST request to the `/products` endpoint. Unlike GET requests, POST requests typically include a request body, which is where the client specifies the attributes of the resource to be created. For example, a POST request to the `/products` endpoint might have a request body that looks like this:
```API
{
"name": "Sneakers",
"color": "blue",
"price": 59.95,
"currency": "USD"
}
```
**PUT** : The PUT method is used to replace an existing resource with an updated version. This method works by replacing the entire resource (i.e., the specific product located at the `/products/123` endpoint) with the data that is included in the request’s body. This means that any fields or properties not included in the request body are deleted, and any new fields or properties are added.
**PATCH** : The PATCH method is used to update an existing resource. It is similar to PUT, except that PATCH enables clients to update specific properties on a resource—without overwriting the others. For instance, if you have a product resource with fields for `name`, `brand`, and `price`, but you only want to update the `price`, you could use the PATCH method to send a request that only includes the new value for the `price` field. The rest of the resource would remain unchanged. This behavior makes the PATCH method more flexible and efficient than PUT.
**DELETE** : The DELETE method is used to remove data from a database. When a client sends a DELETE request, it is requesting that the resource at the specified URL be removed. For example, a DELETE request to the `/products/123` endpoint will permanently remove the product with an `ID` of `123` from the database. Some APIs may leverage authorization mechanisms to ensure that only clients with the appropriate permissions are able to delete resources.
**Which HTTP methods are safe?**
GET is the most commonly used safe method, but the HEAD method—which is used to retrieve only the headers of a resource—is also safe.
## docs
- API Glossary : https://www.postman.com/api-glossary/
- Flask api docs : https://flask.palletsprojects.com/en/stable/api/
- Flask user guide : https://flask.palletsprojects.com/en/stable/
- Flask restful docs : https://flask-restful.readthedocs.io/en/latest/
- Jinja docs : https://jinja.palletsprojects.com/en/stable/
- Template Designer Documentation : https://jinja.palletsprojects.com/en/stable/templates/
- Jinja2 docs : https://www.devdoc.net/python/jinja-2.10.1-doc/
## How to access
- After starting the server, access the endpoints using a web browser or a tool like Postman:
- For restaurant information: `http://127.0.0.1:5000/restaurant`
- For menu items: `http://127.0.0.1:5000/menu/Wings`
## Jinja:
- Jinja2 is a template processing toolkit
- Allows you to create and render text templates
- Integrates well with Flask
- Use to render HTML based on templates
### Tags:
- `{% ... %}` for Statements
- `{{ ... }}` for Expressions to print variable
- `{# ... #}` for Comments not included in the template output
#### Conditoinal:
```
{% if ... %}
...
{% else %}
...
{% endif %}
```
```
{% if score > 80 %}
I'm happy to inform you that you did very well on todays {{ test_name }}.
{% else %}
Your score on today's test {{ test_name }} could have been better. More stduy.
{% endif %}
YOu achieved {{ score }} out of {{ max_score }} points!
```
#### Loops:
```
{% for ... %}
...
{% endfor %}
```
```
topItems = ["Item 2", "Item 3", "Item 7"]
{% for item in topItems %}
{{item}}
{% endfor %}
```