Code Monkey home page Code Monkey logo

hiking-api's Introduction

Building a RESTful API with Flask

Overview

This documentation provides guidelines and steps to build a RESTful API using Flask to manage hiking trails data. The API allows users to retrieve existing trails and add new trail entries.

Prerequisites

  • Python installed on your system
  • Understanding of Python programming
  • Basic knowledge of PostgreSQL and databases

Installation

1. Install Flask

pip install Flask

2. Install psycopg2 for PostgreSQL connection

pip install psycopg2-binary

Setting Up the Database

  • Create a PostgreSQL database named test.
  • Create a table named trails with columns: id (SERIAL), name (VARCHAR), city (VARCHAR), description (TEXT).

Usage

  1. Import necessary modules:

    from flask import Flask, jsonify, request
    import psycopg2
  2. Initialize Flask app and establish PostgreSQL connection:

    app = Flask(__name__)
    # Connect to PostgreSQL
    conn = psycopg2.connect(
        dbname="test",
        user="postgres",
        password="your_password",
        host="localhost",
        port="5432"
    )
  3. Create routes to manage trails:

    • GET /trails: Retrieve all hiking trails.
    • POST /trails: Add a new hiking trail.
  4. Example code snippet for retrieving trails:

    @app.route('/trails', methods=['GET'])
    def get_trails():
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM trails")
        trails = cursor.fetchall()
        trails_data = [{'id': trail[0], 'name': trail[1], 'city': trail[2]} for trail in trails]
        return jsonify({'trails': trails_data})
  5. Example code snippet for adding a new trail:

    @app.route('/trails', methods=['POST'])
    def add_trail():
        new_trail = request.json
        if not new_trail or 'name' not in new_trail or 'city' not in new_trail:
            return jsonify({'error': 'Invalid trail data'}), 400
        cursor = conn.cursor()
        cursor.execute("INSERT INTO trails (name, city) VALUES (%s, %s)", (new_trail['name'], new_trail['city']))
        conn.commit()
        return jsonify({'message': 'New Trail Added Successfully', 'trail': new_trail}), 201
  6. Run the Flask application:

    if __name__ == '__main__':
        app.run(debug=True)

Testing

  • Use tools like Postman or write Python scripts to test API endpoints by sending GET and POST requests to retrieve and add trails.

Security Considerations

  • Implement authentication mechanisms to secure API endpoints and control access.
  • Ensure proper error handling to handle failed requests and validation errors.

hiking-api's People

Contributors

cclint avatar jmartz007 avatar cmiller241 avatar zarbof avatar

Watchers

 avatar

hiking-api's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.