Giter VIP home page Giter VIP logo

py-skygear's Introduction

Python plugin runtime for Skygear

PyPI Build Status License

Example usage right now:

import skygear


@skygear.op("hello:word")
def say(name="world!"):
    return {"message": "hello " + name}


@skygear.handler("chima:echo", auth_required=True)
def meow(payload, io):
    io.write(payload)
    return


@skygear.hook("beforeSave", type="booking", async=False)
def auto_assignment(booking, original_booking, db_conn): # booking is record
    return "No Booking"
    table = db_conn.query("table").filter(status="free").first()
    if table:
        booking.table = create_ref(table.record_id)
    else:
        raise SkygearError("Don't save me")
    return booking


@skygear.before_save("_user", async=False)
def prevent_dead(user, original_user, db_conn):
    return user
    return "Can't dead"


@skygear.before_delete("_user", async=False)
def prevent_dead(user, db_connn):
    raise Exception("can't dead")


@skygear.every(interval=3600)
def generate_daily_report():
    // do something
    return "Ourd will log this bytes to debug level"


@skygear.every("0 0 0 1 * *")
def generate_monthly_report():
    // do something
    return


@skygear.provides("auth", "com.facebook")
class FacebookProvider(skygear.providers.BaseAuthProvider):
    def login(self, auth_data):
        graph = facebook.GraphAPI(access_token=auth_data['access_token'])
        auth_data.update(graph.get_object(id='me'))
        return {"principal_id": auth_data['id'], "auth_data": auth_data}

    def logout(self, auth_data):
        return {"auth_data": auth_data}

    def info(self, auth_data):
        return {"auth_data": auth_data}


@skygear.rest("/path/to/resource", user_required=True)
class RestfulNote(skygear.RestfulRecord):
    record_type = 'note'

if name == "main": skygear.stdin()

Run following will be asyncio process and bind to 0mq

You need to install pyzmq, and respective cbinding

OSX -> brew install zeromq

Debugging your plugin using command line:

export DATABASE_URL=postgres://127.0.0.1/skygear?sslmode=disable
export PUBSUB_URL=ws://localhost:3000/pubsub
echo "{}" | py-skygear sample.py --subprocess init
echo "{}" | py-skygear sample.py --subprocess op hello:word
py-skygear sample.py --subprocess handler chima:echo < < record.json
py-skygear sample.py --subprocess hook book:beforeSave < record.json
py-skygear sample.py --subprocess hook _user:beforeDelete < user.json
echo "{}" | py-skygear sample.py --subprocess timer plugin.generate_daily_report
echo "{}" | py-skygear sample.py --subprocess timer plugin.generate_monthly_report

Since database url is read from environment variable, you have to start Ourd specifying the database connection string:

DATABASE_URL=postgresql://localhost/skygear?sslmode=disable py-skygear sample.py --skygear-address tcp://127.0.0.1:5555 --skygear-endpoint http://127.0.0.1:3000 --apikey=API_KEY

py-skygear's People

Contributors

cheungpat avatar limouren avatar rickmak avatar

Watchers

 avatar

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.