Skip to content

Instantly share code, notes, and snippets.

@munen
Created May 1, 2013 15:22
Show Gist options
  • Select an option

  • Save munen/5495943 to your computer and use it in GitHub Desktop.

Select an option

Save munen/5495943 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from flask import Flask
from flask.helpers import url_for
from flask import request
import json
import re
app = Flask(__name__)
# helper method to extract parameters. reqest to api has the following format:
# /(endpoint)?filter_by[(filter_by_parameter)]=(keyword)
#
# example:
# /geodata_structures?filter_by[field_name]=sample%20field_name
def read_params(params):
filter_by = re.search(r'filter_by\[(.*)\]', params).group(1)
param_key = re.search(r'filter_by\[.*\]', params).group()
keyword = request.args.get(param_key, '')
return filter_by, keyword
@app.route('/geodata_elements', methods=['GET'])
def geodata_elements():
filter_by, keyword = read_params(request.args.keys()[0])
# TODO: Replace sample_data dict with database cache
sample_data = [ {
"name": "JR_REVIER_F",
"alias": "Jagdreviergrenzen (Layer)",
"type_of_features": "Layer",
"path": "file://foo"
},
{
"name": "ANJF_FREIGABE_JR_REVIER_F",
"alias": "Jagdreviergrenzen (Flächen)",
"type_of_features": "Polygon Features",
"path": "file://foo"
} ]
geodata_elements = [gs for gs in sample_data if keyword in gs[filter_by] ]
return json.dumps(geodata_elements)
@app.route('/geodata_structures', methods=['GET'])
def geodata_structures():
filter_by, keyword = read_params(request.args.keys()[0])
# TODO: Replace sample_data dict with database cache
sample_data = [{
'field_name' : 'sample field_name',
'alias' : 'sample alias',
'data_type' : 'sample data_type',
'unit' : 'sample unit',
'description' : 'sample description',
'geodata_element_name' : 'JR_REVIER_F',
}]
geodata_structures = [gs for gs in sample_data if keyword in gs[filter_by] ]
return json.dumps(geodata_structures)
if __name__ == "__main__":
with app.test_request_context():
print url_for('geodata_elements', param='')
print url_for('geodata_structures', param='')
app.debug = True
app.run(host='127.0.0.1', port=3999)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment