templates icon indicating copy to clipboard operation
templates copied to clipboard

feat: Add Python URL shortener template

Open trivedikavya opened this issue 4 months ago • 1 comments

What does this PR do?

This PR adds a new serverless function template for a URL shortener using the Python runtime. This function allows users to:

  1. Create a unique, short ID for any given long URL.
  2. Handle HTTP redirections from the short URL to the original long URL.

The implementation uses the nanoid library for generating short IDs and the official appwrite-server-sdk for database interactions, making it a robust and practical example for the templates collection.

Test Plan

The function has been tested locally by following these steps:

1. Appwrite Setup:

  • Created a new database in an Appwrite project.
  • Created a new collection within the database.
  • Added a single String Attribute to the collection with the key original_url, a size of 2048, and marked it as required.
  • Saved the Database ID and Collection ID for the environment variables.

2. Function Deployment:

  • Navigated to the python/url-shortener/ directory.
  • Created a compressed code.tar.gz file of the source code.
  • Created a new function in the Appwrite console and uploaded the code.tar.gz file.
  • Added the following environment variables in the function's settings:
    • APPWRITE_ENDPOINT
    • APPWRITE_API_KEY
    • APPWRITE_PROJECT
    • DATABASE_ID (from step 1)
    • COLLECTION_ID (from step 1)
  • Deployed the function.

3. Execution and Verification:

  • Test Case 1: Creating a Short URL

    • Executed the function with the following JSON payload:
      {
        "url": "[https://appwrite.io/](https://appwrite.io/)"
      }
      
    • Result: The function executed successfully and returned a JSON response containing the short_url, confirming the document was created in the database.
  • Test Case 2: Redirecting to the Original URL

    • Copied the document ID (the short ID) from the result of the previous test.
    • Executed the function again with the following JSON payload:
      {
        "short_id": "[COPIED_SHORT_ID_HERE]"
      }
      
    • Result: The function correctly issued a 301 redirect to the original URL (https://appwrite.io/), verifying the lookup and redirect logic.

Related PRs and Issues

None.

Have you read the Contributing Guidelines on issues?

Yes.

Summary by CodeRabbit

  • New Features

    • Introduced a URL Shortener service to create short links and resolve them to original destinations (create short URLs, fetch and redirect).
  • Documentation

    • Added setup and deployment guide with required environment variables and build/deploy steps.
    • Included usage examples showing JSON requests to create and retrieve shortened URLs.

trivedikavya avatar Oct 16 '25 18:10 trivedikavya

Walkthrough

Adds a Python URL Shortener for Appwrite: a README documenting env vars and deploy/usage; requirements.txt pinning appwrite==3.1.0 and nanoid==2.0.0; src/utils.py providing get_database() which validates APPWRITE_ENDPOINT, APPWRITE_PROJECT, and APPWRITE_API_KEY and returns a cached Appwrite Databases client; src/main.py adding main(context) that handles GET/HEAD redirects (path or query id), JSON lookups by short_id, and URL creation via url in JSON (validates URL, generates a 7-character nanoid with up to 5 collision retries, stores documents in Appwrite, and returns the constructed short URL). Error handling covers missing env vars, invalid JSON, validation errors, Appwrite 404/409, and other Appwrite errors.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The PR title "feat: Add Python URL shortener template" directly and accurately captures the primary change of this pull request. The changeset adds a complete Python URL shortener template consisting of documentation, dependencies, and implementation code (main.py with URL shortening and redirect logic, plus utils.py for database utilities). The title is concise, clear, and specific—it uses the conventional commit format (feat:) appropriately and conveys exactly what a teammate scanning the repository history would expect: the addition of a new URL shortener template in Python.
✨ Finishing touches
  • [ ] 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Oct 16 '25 18:10 coderabbitai[bot]