Skip to content

Instantly share code, notes, and snippets.

@joeclark-phd
Last active May 10, 2016 23:12
Show Gist options
  • Save joeclark-phd/5cbcdbdf070d89abaec0 to your computer and use it in GitHub Desktop.
Save joeclark-phd/5cbcdbdf070d89abaec0 to your computer and use it in GitHub Desktop.
app.py for Bluemix flinkedin with Pymongo
import os
from flask import Flask, jsonify
app = Flask(__name__)
from pymongo import MongoClient
# insert your connection details here
MONGO_URL = 'mongodb://<dbuser>:<pass>@<database URL>'
# connect to the MongoDB server
client = MongoClient(MONGO_URL)
# connect to the default database within the server
db = client.get_default_database()
from bson import json_util
@app.route("/", methods=['GET'])
def hello_world():
return( "Hello world. Try /api/profiles or /api/companies for data." )
# listing all profiles
@app.route("/api/profiles", methods=['GET'])
def list_profiles():
real_data = db.profiles.find()
return( json_util.dumps({"profiles":real_data}) )
# retrieve one profile
@app.route("/api/profile/<string:id>", methods=['GET'])
def one_profile(id):
real_data = db.profiles.find({"_id":id})
return( json_util.dumps({"profile":real_data}) )
# listing all companies
@app.route("/api/companies", methods=['GET'])
def list_companies():
data = db.proiles.distinct("jobs.employer")
return( jsonify({"companies":data}) )
# list all profiles for one company
@app.route("/api/company/<string:company>", methods=['GET'])
def one_company(company):
data = {"company": company,
"profiles": list(db.profile.find({"jobs.employer":company},{"name":1}))
}
return( jsonify(data) )
if __name__ == "__main__":
port = int(os.environ.get("VCAP_APP_PORT", 5000))
app.run(host='0.0.0.0', port=port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment