Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added example HRX, #4

  • Loading branch information...
commit 15929bc79a4bf7d753ca1355e7adf43df2ad3a2f 1 parent 1f844ea
@nycholas nycholas authored
View
3  examples/hrx/api/__init__.py
@@ -0,0 +1,3 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2012-2013, Cenobit Technologies, Inc. http://cenobit.es/
+# All rights reserved.
View
17 examples/hrx/api/hello.py
@@ -0,0 +1,17 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2012-2013, Cenobit Technologies, Inc. http://cenobit.es/
+# All rights reserved.
+from flask import Blueprint
+
+from hrx import jsonrpc
+
+mod = Blueprint('hello', __name__)
+jsonrpc.register_blueprint(mod)
+
+@jsonrpc.method('hello.index')
+def index():
+ return 'Welcome to Hello API!'
+
+@jsonrpc.method('hello.say(name=String)')
+def say(name=''):
+ return 'Hello {0}!'.format(name)
View
33 examples/hrx/hrx.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright (c) 2012-2013, Cenobit Technologies, Inc. http://cenobit.es/
+# All rights reserved.
+import os
+import sys
+
+from flask import Flask, render_template
+
+PROJECT_DIR, PROJECT_MODULE_NAME = os.path.split(
+ os.path.dirname(os.path.realpath(__file__))
+)
+
+FLASK_JSONRPC_PROJECT_DIR = os.path.join(PROJECT_DIR, os.pardir)
+if os.path.exists(FLASK_JSONRPC_PROJECT_DIR) \
+ and not FLASK_JSONRPC_PROJECT_DIR in sys.path:
+ sys.path.append(FLASK_JSONRPC_PROJECT_DIR)
+
+from flask_jsonrpc import JSONRPC
+
+app = Flask(__name__)
+app.config.from_object(__name__)
+jsonrpc = JSONRPC(app, '/api')
+
+@app.route('/')
+def index():
+ return render_template('index.html')
+
+import api.hello
+
+
+if __name__ == '__main__':
+ app.run(debug=True)
View
50 examples/hrx/static/js/api.js
@@ -0,0 +1,50 @@
+(function(win) {
+ 'use strict';
+
+ var $jsonrpc = function(config) {
+ var self = this;
+ self.defaults = {
+ url: 'http://' + window.location.host + '/api',
+ service: undefined,
+ version: '2.0'
+ };
+ $.extend(true, self.defaults, config || {});
+
+ self.UUID = function() {
+ // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);});
+ };
+
+ self.request = function(name, params) {
+ if (!!self.defaults.service) {
+ name = self.defaults.service + '.' + name;
+ }
+ var deferred = $.ajax({
+ type: 'POST',
+ dataType: 'json',
+ url: self.defaults.url,
+ data: JSON.stringify({
+ 'jsonrpc': self.defaults.version,
+ 'method': name,
+ 'params': params,
+ 'id': self.UUID()
+ }),
+ cache: false
+ });
+
+ deferred.done(function(data, textStatus, jqXHR) {
+ if ('error' in data) {
+ var code = data['error']['code'];
+ if (code >= -32768 || code <= -32000) {
+ throw data['error']['message'];
+ }
+ }
+ });
+
+ return deferred;
+ };
+ };
+
+ win.$jsonrpc = $jsonrpc;
+
+})(window);
View
42 examples/hrx/templates/index.html
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="utf-8" />
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
+
+ <title>Flask-JSONRPC</title>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ <meta name="description" content="Flask-JSONRPC" />
+ <meta name="author" content="Cenobit Technologies, Inc. http://cenobit.es/" />
+</head>
+<body>
+ <div id="form">
+ <label for="name">Name:</label>
+ <input id="name" name="name" type="text" placeholder="Enter a name here">
+ <input type="button" name="btn-send" id="btn-send" value="Send" />
+ <hr>
+ <h1 id="message"></h1>
+ </div>
+
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
+ <script type="text/javascript" src="{{ url_for('static', filename='js/api.js') }}"></script>
+ <script type="text/javascript">
+ $(document).ready(function($) {
+ var jsonrpc = new $jsonrpc({url: 'http://localhost:5000/api', version: '2.0'});
+
+ $('#form')
+ .undelegate('#btn-send', 'click')
+ .delegate('#btn-send', 'click', function(env) {
+ jsonrpc.request('hello.say', {name: $('#name').val()}).done(function(data) {
+ $('#message').text(data.result);
+ });
+ });
+
+ jsonrpc.request('hello.index').done(function(data) {
+ $('#message').text(data.result);
+ });
+ });
+ </script>
+</body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.