Konubinix' opinionated web of thoughts

Use Gapi in Flexx

Fleeting

use gapi in flexx

from flexx import flx

flx.assets.associate_asset(
    __name__,
    "https://apis.google.com/js/api.js",
)

class Gapi(flx.HBox):
    signed_in = flx.BoolProp(settable=True)
    api_key = flx.StringProp(settable=True)
    client_id = flx.StringProp(settable=True)
    discovery_docs = flx.ListProp(settable=True)
    scopes = flx.StringProp(settable=True)

    def init(self):
        self.button = flx.Button()
        self.api = window.gapi
        window.gapi.load('client:auth2', self.init_client)

    def init_client(self):
        window.gapi.client.init({
            "apiKey": self.api_key,
            "discoveryDocs": self.discovery_docs,
            "clientId": self.client_id,
            "scope": self.scopes
        }).then(self.client_inited, self.root.frontend.debug)

    def client_inited(self):
        self.api.auth2.getAuthInstance().isSignedIn.listen(self.set_signed_in)
        self.set_signed_in(self.api.auth2.getAuthInstance().isSignedIn.get())

    @flx.reaction("signed_in")
    def _update_button_style(self, *ev):
        self.button.set_text("Sign out" if self.signed_in else "Sign in")

    @flx.reaction("button.pointer_click")
    def _click(self, *ev):
        if self.signed_in:
            self.api.auth2.getAuthInstance().signOut()
        else:
            self.api.auth2.getAuthInstance().signIn()