Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- Software Laboratory 5
- SOA service demo
- This sample service was created for the purposes of demonstrating some
- of the functionality you can achieve by combining the power of three
- Python libraries: cx_Oracle, Flask, and Requests.
- It does not intend to be perfect Python code -- in some places, perfection
- was traded for simplicity, some of these are marked in comments.
- This comment is a so-called docstring, all Python modules and
- functions/methods should have one. Three " or ' characters make it
- possible for multiline strings, and interactive Python environments
- display these "docstrings" (basically header comments) for users of
- your code. Further info: http://www.python.org/dev/peps/pep-0257/
- """
- from flask import Flask, jsonify, abort, request
- from datetime import datetime
- import json
- import cx_Oracle
- import requests
- app = Flask(__name__)
- @app.route('/cegek.json')
- def list_people():
- """Lists the first 50 persons in the database"""
- conn = get_db()
- try:
- cur = conn.cursor()
- try:
- cur.execute('SELECT id, nev, kapcsolattarto FROM fof1so.cegek')
- results = []
- for id, nev, kapcsolattarto in cur:
- results.append({'id': id, 'nev': nev, 'kapcsolattarto': kapcsolattarto})
- return jsonify(cegek=results)
- finally:
- cur.close()
- finally:
- conn.close()
- @app.route('/cegek/<id>.json')
- def show_person(id):
- """Shows the details of a single person by szemelyi_szam"""
- conn = get_db()
- try:
- cur = conn.cursor()
- try:
- cur.execute('SELECT id,nev,bankszamla,kapcsolattarto FROM fof1so.cegek WHERE id = :cegid1',
- cegid1=id)
- result = cur.fetchone()
- if result is None:
- abort(404)
- else:
- id,nev,bankszamla,kapcsolattarto = result
- res = requests.get("https://seeks.hsbp.org",params={
- 'format': 'json',
- 'q': result[0]
- })
- print(res.url)
- print(res.text)
- print(res.json)
- results.append({'id': id, 'nev': nev, 'bankszamla':bankszamla, 'kapcsolattarto': kapcsolattarto})
- """return jsonify(cegek=result)"""
- return jsonify(res[0])
- except: pass
- finally:
- cur.close()
- finally:
- conn.close()
- @app.route('/datetest.json')
- def date_test():
- conn = get_db()
- try:
- cur = conn.cursor()
- try:
- # http://www.oracle.com/technetwork/articles/dsl/prez-python-timesanddates-093014.html
- # https://docs.python.org/2/library/datetime.html
- # its casted automatically to datetime
- cur.execute('SELECT datum, usd FROM oktatas.mnb_deviza where id < 10')
- results = []
- for datum, usd in cur:
- results.append({'datum': datum, 'datum_iso' : datum.isoformat(), 'usd': usd})
- return jsonify(arfolyamok=results)
- finally:
- cur.close()
- finally:
- conn.close()
- @app.route('/verbtest.json', methods=['PUT', 'POST'])
- def verb_test():
- """Lets you test HTTP verbs different from GET, expects and returns data in JSON format"""
- # it also shows you how to access the method used and the decoded JSON data
- return jsonify(method=request.method, data=request.json, url=request.url)
- def get_db():
- """Connects to the RDBMS and returns a connection object"""
- # when used with a `file` object, `with` ensures it gets closed
- with file('config.json') as config_file:
- config = json.load(config_file)
- return cx_Oracle.connect(config['user'], config['pass'], config['host'])
- if __name__ == "__main__":
- import os
- os.environ['NLS_LANG'] = '.UTF8'
- app.run(debug=True, port=os.getuid() + 10000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement