def test_valid_firebase_url(self): test_urls = [ 'https://validurl.firebaseio.com', 'https://anothervalidurl.firebaseio.com/', 'https://another-validurl98.firebaseio.com/' ] for url in test_urls: self.assertTrue(Firebase.is_valid_firebase_url(url))
class TestFirebase(TestCase): def setUp(self): self.firebase = Firebase(TEST_FIREBASE_URL) self.assertIsInstance(self.firebase, Firebase) def test_ref(self): node_ref = self.firebase.ref('test_ref') self.assertIsInstance(node_ref, FirebaseReference) self.assertEqual(node_ref.current, 'test_ref') def test_child(self): node_ref = self.firebase.ref('test_ref') self.assertEqual(node_ref.current, 'test_ref') child_ref = node_ref.child('test_child_ref') self.assertEqual(child_ref.current, 'test_ref/test_child_ref') def test_current_url_property(self): node_ref = self.firebase.ref('test_ref') self.assertEqual(node_ref.current_url, "{}/{}.json".format( TEST_FIREBASE_URL, 'test_ref') ) @mock.patch('requests.get') def test_get_event(self, mock_requests_get): node_ref = self.firebase.ref('test_ref') results = node_ref.get() self.assertTrue(mock_requests_get.called) mock_requests_get.assert_called_with(node_ref.current_url) @mock.patch('requests.put') def test_set_event(self, mock_requests_put): node_ref = self.firebase.ref('test_ref') result = node_ref.set('val') self.assertTrue(mock_requests_put.called) mock_requests_put.assert_called_with(node_ref.current_url, json='val') @mock.patch('requests.post') def test_push_event(self, mock_requests_post): node_ref = self.firebase.ref('test_ref') result = node_ref.push('val') self.assertTrue(mock_requests_post.called) mock_requests_post.assert_called_with(node_ref.current_url, json='val') @mock.patch('requests.delete') def test_delete_event(self, mock_requests_delete): node_ref = self.firebase.ref('test_ref') result = node_ref.delete() self.assertTrue(mock_requests_delete.called) mock_requests_delete.assert_called_with(node_ref.current_url)
class TestFirebase(TestCase): def setUp(self): self.firebase = Firebase(TEST_FIREBASE_URL) self.assertIsInstance(self.firebase, Firebase) def test_ref(self): node_ref = self.firebase.ref('test_ref') self.assertIsInstance(node_ref, FirebaseReference) self.assertEqual(node_ref.current, 'test_ref') def test_child(self): node_ref = self.firebase.ref('test_ref') self.assertEqual(node_ref.current, 'test_ref') child_ref = node_ref.child('test_child_ref') self.assertEqual(child_ref.current, 'test_ref/test_child_ref') def test_current_url_property(self): node_ref = self.firebase.ref('test_ref') self.assertEqual(node_ref.current_url, "{}/{}.json".format(TEST_FIREBASE_URL, 'test_ref')) @mock.patch('requests.get') def test_get_event(self, mock_requests_get): node_ref = self.firebase.ref('test_ref') results = node_ref.get() self.assertTrue(mock_requests_get.called) mock_requests_get.assert_called_with(node_ref.current_url) @mock.patch('requests.put') def test_set_event(self, mock_requests_put): node_ref = self.firebase.ref('test_ref') result = node_ref.set('val') self.assertTrue(mock_requests_put.called) mock_requests_put.assert_called_with(node_ref.current_url, json='val') @mock.patch('requests.post') def test_push_event(self, mock_requests_post): node_ref = self.firebase.ref('test_ref') result = node_ref.push('val') self.assertTrue(mock_requests_post.called) mock_requests_post.assert_called_with(node_ref.current_url, json='val') @mock.patch('requests.delete') def test_delete_event(self, mock_requests_delete): node_ref = self.firebase.ref('test_ref') result = node_ref.delete() self.assertTrue(mock_requests_delete.called) mock_requests_delete.assert_called_with(node_ref.current_url)
def setUp(self): self.firebase = Firebase(TEST_FIREBASE_URL) self.assertIsInstance(self.firebase, Firebase)
# -*- coding: utf-8 -*- #匯入firebase模組 from pyfirebase import Firebase #指定firebase URL路徑 firebase = Firebase("https://changyee-1.firebaseio.com") #指定資料層級 firebase_ref = firebase.ref("TW-CY/Time") #Read Event Time Event_Time = firebase_ref.get() print Event_Time
import microgear.client as microgear import logging from pyfirebase import Firebase import time import json import re from datetime import datetime firebase = Firebase('https://iotapplication-7cf10.firebaseio.com/') ref = firebase.ref('CustomerInfo') ref1 = firebase.ref('CustomerInfo') itemlist = [] itemlist_key = [] CustomerAddress = '' appid = 'SmartFridge' gearkey = 'YpZmMdcaemYKQLb' gearsecret = 'I10EsM0ZTaI4PbDVHAWm3j96G' microgear.create(gearkey, gearsecret, appid, {'debugmode': True}) itemRef = ref.child('-KqsdeuVyyatxKELuMs4').child('Item').get() InfoRef = ref1.child('-KqsdeuVyyatxKELuMs4').get() def RetreiveData(): # Get the contents from the reference a = []
import sys import signal from pyfirebase import Firebase firebase = Firebase('https://apitest-72809.firebaseio.com/') # Create a reference to the root by not passing in paramters to the ref function root = firebase.ref() # This function will be called everytime the event happens def print_data(event, data): print event print data # We can the event listener to the root ref. We also specify that print_data should be called on event root.on('child_changed', callback=print_data) # Extra logic to turn on listener when we quit def signal_handler(signal, frame): print "Trying to exit" root.off() sys.exit(0) # Binding Ctrl + C signal to signal_handler signal.signal(signal.SIGINT, signal_handler) signal.pause()
from pyfirebase import Firebase firebase = Firebase('https://apitest-72809.firebaseio.com/') # Create a Firebase reference ref = firebase.ref('books') # Get the contents of the reference books = ref.get() # Payload data can be declared using native python data types payload = {'name': 'Harry Potter and the Prisoner of Azkaban', 'pages': 780} # The push operation pushes a new node under this node book = ref.push(payload) # We can get the new node id using the name key id = book['name'] # We can navigate to child nodes bookref = ref.child(id) # We can update specific records in this ref bookref.update({'pages': 600}) # We can also just outright replace the content bookref.set({'name': 'Harry Potter and the Order of the Phoenix', 'pages': 980}) # We can obviously simulate an update with a set bookref.child('pages').set(790)
from flask import render_template, request from flask_api import FlaskAPI, status, exceptions from flask_api.decorators import set_renderers from flask_api.renderers import JSONRenderer, HTMLRenderer from pyfirebase import Firebase import time app = FlaskAPI(__name__, template_folder='.', static_folder='build') firebase = Firebase('https://6ixtests.firebaseio.com') @app.route('/scorer/<string:exam_id>/', methods=['POST']) @set_renderers(JSONRenderer) def scorer(exam_id): score = 0 report_card = {'responses': {}} resp = request.get_json() user_responses = resp['data'] user_id = resp['uid'] answers_ref = firebase.ref('questions/{}/answers'.format(exam_id)) answers = answers_ref.get() for q_id, correct_answer in answers.iteritems(): # Convert to actual strings q_id, correct_answer = map(str, [q_id, correct_answer]) status = {'is_correct': False} try: if user_responses[q_id] == correct_answer: status['is_correct'] = True score += 1