Skip to main content

Google Cloud functions wrapper

Project description

GOBLET

PyPI PyPI - Python Version Tests codecov

Goblet is a framework for writing serverless rest apis in python in google cloud. It allows you to quickly create and deploy python apis backed by cloudfunctions.

It provides:

  • A command line tool for creating, deploying, and managing your api
  • A decorator based API for integrating with GCP API Gateway, Storage, Cloudfunctions, PubSub, Scheduler, and other GCP services.
  • Local environment for your api endpoints
  • Dynamically generated openapispec
  • Support for multiple stages

You can create Rest APIs:

from goblet import Goblet, jsonify

app = Goblet(function_name="goblet_example")

@app.route('/home')
def home():
    return {"hello": "world"}

@app.route('/home/{id}', methods=["POST"])
def post_example(id: int) -> List[int]:
    return jsonify([id])

Once you've written your code, you just run goblet deploy and Goblet takes care of deploying your app.

$ goblet deploy
...
https://api.uc.gateway.dev

$ curl https://api.uc.gateway.dev/home
{"hello": "world"}

Installation

To install goblet, open an interactive shell and run:

pip install goblet-gcp

Make sure to have the correct services enabled in your gcp project depending on what you want to deploy

api-gateway, cloudfunctions, storage, pubsub, scheduler

You will also need to install gcloud cli for authentication

QuickStart

In this tutorial, you'll use the goblet command line utility to create and deploy a basic REST API. This quickstart uses Python 3.7. You can find the latest versions of python on the Python download page.

To install Goblet, we'll first create and activate a virtual environment in python3.7:

$ python3 --version
Python 3.7.3
$ python3 -m venv venv37
$ . venv37/bin/activate

Next we'll install Goblet using pip:

python3 -m pip install goblet-gcp

You can verify you have goblet installed by running:

$ goblet --help
Usage: goblet [OPTIONS] COMMAND [ARGS]...
...

Credentials

Before you can deploy an application, be sure you have credentials configured. You should run gcloud auth application-default login and sign in to the desired project.

When setting the defaut location note tha api-gateway is only available in asia-east1, europe-west1, us-east-1 and us-central1.

Creating Your Project

create your project directory, which should include an main.py and a requirements.txt. Make sure requirements.txt includes goblet-gcp

$ ls -la
drwxr-xr-x   .goblet
-rw-r--r--   main.py
-rw-r--r--   requirements.txt

You can ignore the .goblet directory for now, the two main files we'll focus on is app.py and requirements.txt.

Let's take a look at the main.py file:

from goblet import Goblet

app = Goblet(function_name="goblet_example")

@app.route('/home')
def index():
    return {"hello": "world"}

This app will deploy an api with endpoint /home.

Running Locally

Running your functions locally for testing and debugging is easy to do with goblet. First set a local param in the goblet class

from goblet import Goblet

app = Goblet(function_name="goblet_example", local='test')

Then run goblet local test and replace test with whatever variable you decide to use. Now you can hit your functions endpoint at localhost:8080 with your routes.

Deploying

Let's deploy this app. Make sure you're in the app directory and run goblet deploy:

$ goblet deploy
INFO:goblet.deployer:zipping function......
INFO:goblet.deployer:uploading function zip to gs......
INFO:goblet.deployer:creating google function......
INFO:goblet.deployer:deploying api......
INFO:goblet.deployer:api successfully deployed...
INFO:goblet.deployer:api endpoint is goblet-example-yol8sbt.uc.gateway.dev

You now have an API up and running using API Gateway and cloudfunctions:

$ curl https://goblet-example-yol8sbt.uc.gateway.dev/home
{"hello": "world"}

Try making a change to the returned dictionary from the home() function. You can then redeploy your changes by running golet deploy.

Next Steps

You've now created your first app using goblet. You can make modifications to your main.py file and rerun goblet deploy to redeploy your changes.

At this point, there are several next steps you can take.

Docs - Goblet Documentation

If you're done experimenting with Goblet and you'd like to cleanup, you can use the goblet destroy command, and Goblet will delete all the resources it created when running the goblet deploy command.

$ goblet destroy
INFO:goblet.deployer:destroying api gateway......
INFO:goblet.deployer:api configs destroying....
INFO:goblet.deployer:apis successfully destroyed......
INFO:goblet.deployer:deleting google cloudfunction......
INFO:goblet.deployer:deleting storage bucket......

Docs

Goblet Documentation

Blog Posts

Building Python Serverless Applications on GCP

Examples

Goblet Examples

Issues

Please file any issues, bugs or feature requests as an issue on our GitHub page.

Roadmap

☑ Integration Tests
Api Gateway Auth
☑ Configuration Options (function names, ...)
☑ Use checksum for updates
☐ Generate Openapi clients
☑ User generated dataclasses for openapi spec
Scheduler trigger
Pub Sub trigger
Cloud Storage trigger
Firestore trigger
Firebase trigger
Cloud Tasks trigger
Cloud Endpoints trigger

Want to Contribute

If you would like to contribute to the library (e.g. by improving the documentation, solving a bug or adding a cool new feature) submit a pull request.

Want to Support

Buy Me A Coffee


Based on chalice

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

goblet-gcp-0.4.8.1.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

goblet_gcp-0.4.8.1-py3-none-any.whl (26.9 kB view details)

Uploaded Python 3

File details

Details for the file goblet-gcp-0.4.8.1.tar.gz.

File metadata

  • Download URL: goblet-gcp-0.4.8.1.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8

File hashes

Hashes for goblet-gcp-0.4.8.1.tar.gz
Algorithm Hash digest
SHA256 704ba8b068b4240b0e0f820989236b0391e9a9493b3df060f3d9d0a9973d8c94
MD5 b78781c83fd43eb0e99ce474ef87f7b5
BLAKE2b-256 1822f7ce35f62e82c9c3dd010ea5b3ed82de0e0e8d445c1f7baaefa862f28593

See more details on using hashes here.

File details

Details for the file goblet_gcp-0.4.8.1-py3-none-any.whl.

File metadata

  • Download URL: goblet_gcp-0.4.8.1-py3-none-any.whl
  • Upload date:
  • Size: 26.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8

File hashes

Hashes for goblet_gcp-0.4.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6388aef6776c1da8a4e514f55f3f870ada6521ac28a79e0a41d6dd037d41666c
MD5 8b8ec6a2fa9e2e4788d756ab673475b8
BLAKE2b-256 71f3f796b133ceb4855540493a8a91e105e02d3f3941f23d283aa03dbb7ab771

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page