I have this app
import braintree
from flask import Flask, render_template, send_from_directory, request
try:
from ConfigParser import SafeConfigParser
except ImportError:
from configparser import SafeConfigParser
app = Flask(__name__)
parser = SafeConfigParser()
parser.read('secrets.ini')
MERCHANTID = parser.get('braintree', 'MERCHANTID')
PUBLICKEY = parser.get('braintree', 'PUBLICKEY')
PRIVATEKEY = parser.get('braintree', 'PRIVATEKEY')
braintree.Configuration.configure(braintree.Environment.Sandbox,
merchant_id=MERCHANTID,
public_key=PUBLICKEY,
private_key=PRIVATEKEY)
#app.route("/")
def index():
# Generate client token for the dropin ui
client_token = braintree.ClientToken.generate({})
return render_template('index.html', token=client_token)
#app.route("/proc", methods=['GET', 'POST'])
def proc():
# result = braintree.Transaction.sale({
# "amount": request.form["amount"],
# "payment_method_nonce": request.form["payment_method_nonce"]
# })
# print(result)
result = braintree.Customer.create({
"credit_card": {
"number": "4111111111111111",
"expiration_date": "12/16"
}
})
result = braintree.Subscription.create({
"payment_method_token": result.customer.credit_cards[0].token,
"plan_id": "upwork_sub"
})
return render_template('proc.html', result=result, request=request.form)
if __name__ == "__main__":
app.run(host='0.0.0.0')
The one time charge(the code that is commented) works fine.
The problem is when I want to charge with subscription I have to hard create the Client and then auto charge with subscription.
How I can make it without hard coding the clients info
Related
I would like to connect to spotify API and connect with OAuth and then search for a song a user requests.
I currently have a bot pulling for youtube and saving the file to play. I was wondering if there a way to do the same with Spotipy.
from flask import Flask, request, url_for, session, redirect
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import time
app = Flask(__name__)
app.secret_key = 'xxx'
app.config['SESSION_COOKIE_NAME'] = 'Token Cookie'
TOKEN_INFO = "token_info"
#app.route('/')
def login():
sp_oauth = create_spotify_oauth()
auth_url = sp_oauth.get_authorize_url()
return redirect(auth_url)
#app.route('/redirect')
def redirectPage():
sp_oauth = create_spotify_oauth()
session.clear()
code = request.args.get('code')
token_info = sp_oauth.get_access_token(code)
session[TOKEN_INFO] = token_info
return redirect(url_for('getTracks', _external=True))
#app.route('/getTracks')
def getTracks():
try:
token_info = get_token()
except:
print("user not logged in)")
redirect("/")
sp = spotipy.Spotify(auth = token_info["access_token"])
return
def get_token():
token_info = session.get(TOKEN_INFO, None)
if not token_info:
raise "execption"
now = int(time.time())
is_expired = token_info["expires_at"] - now < 60
if (is_expired):
sp_oauth = create_spotify_oauth()
token_info = sp_oauth.refresh_access_token(token_info['refresh_token'])
return token_info
def create_spotify_oauth():
return SpotifyOAuth(
client_id = 'CLIENT_ID',
client_secret = 'CLIENT_SECRET',
redirect_uri = url_for('redirectPage', _external = True),
scope= 'user-library-read')
I'm trying to create a simple API and server with MongoDB and Flask in python(pycharm). I'm testing the methods with Postman and so far the GET and DELETE methods work. I'm having troubles with the POST method mainly(for adding an entity). I have 2 classes
repository class
class ExercisesRepository:
def __init__(self):
self.client = MongoClient('localhost', 27017)
self.client.drop_database("exercise_database") # clear everything that was before
self.db = self.client.exercise_database # create database
self.exercises = self.db.exercises # create table in the database
def get_all(self):
return [{
'id': str(exercise['_id']),
'type': exercise['type'],
'calories': exercise['calories']
} for exercise in self.exercises.find()]
def add(self, exercise):
exercise = {key: exercise[key] for key in exercise}
exercise['calories'] = int(exercise['calories']) #line 24
self.exercises.insert_one(exercise) # automatically generates an ObjectId for the exercise
return 200
def update(self, exercise_id, exercise):
my_query = {"_id": ObjectId(exercise_id)}
new_values = {"$set": {"type": exercise["type"], "calories": exercise["calories"]}}
self.exercises.update_one(my_query, new_values)
return 200
def delete(self, exercise_id):
self.exercises.remove(ObjectId(exercise_id))
return 200
def check_database_content(self):
for exercise in self.exercises.find():
pprint.pprint(exercise)
server class
from ExercisesRepository import ExercisesRepository
from flask import Flask
from flask import request
from flask import jsonify
import sys
app = Flask(__name__)
exerciseRepo = ExercisesRepository()
exerciseRepo.add({'type': 'Yoga', 'calories': 500})
exerciseRepo.add({'type': 'Walk', 'calories': 300})
exerciseRepo.add({'type': 'Run', 'calories': 100})
#app.route('/')
def hello_world():
return 'Hello World!'
#app.route("/exercises", methods=['GET', 'POST'])
def exercises():
if request.method == 'GET':
return jsonify(exerciseRepo.get_all())
elif request.method == 'POST':
print(request.form, file=sys.stderr)
return jsonify(exerciseRepo.add(request.form)) #line 31
#app.route('/exercises/<exercise_id>', methods=['PUT', 'DELETE'])
def exercises_id(exercise_id):
if request.method == 'PUT':
print(request.form, file=sys.stderr)
return jsonify(exerciseRepo.update(exercise_id, request.form))
elif request.method == 'DELETE':
return jsonify(exerciseRepo.delete(exercise_id))
if __name__ == '__main__':
app.run()
When I try to make a POST call in postman with a JSON like this :
{
"type": "Aerobic",
"calories": 500
} I get the following message in postman: 500 Internal Server Error
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
and in Pycharm console:
File "server.py", line 31, in exercises
return jsonify(exerciseRepo.add(request.form))
server\ExercisesRepository.py", line 24, in add
exercise['calories'] = int(exercise['calories'])
KeyError: 'calories'
127.0.0.1 - - [05/Jan/2020 13:01:50] "POST /exercises HTTP/1.1" 500 -
I'm pretty new to python and this is my first try to make an api so if you could explain as much as possible it would be very helpful. Thanks!
If you send data as JSON then you have to get it using request.json, not request.form
from flask import Flask, request, jsonify
app = Flask(__name__)
#app.route("/exercises", methods=['GET', 'POST'])
def exercises():
print('form:', request.form)
print('json:', request.json)
return jsonify(request.json) #line 31
if __name__ == '__main__':
app.run()
When you send as JSON
import requests
r = requests.post('http://localhost:5000/exercises', json={'type': 'Run', 'calories': 100})
then server shows
form: ImmutableMultiDict([])
json: {'type': 'Run', 'calories': 100}
When you send as form data
import requests
r = requests.post('http://localhost:5000/exercises', data={'type': 'Run', 'calories': 100})
then server shows
form: ImmutableMultiDict([('type', 'Run'), ('calories', '100')])
json: None
form sends data as string type=Run&calories=100 in body,
json sends data as string {'type': 'Run', 'calories': 100} in body.
You can see it if you display request's body
import requests
r = requests.post('https://httpbin.org/post', data={'type': 'Run', 'calories': 100})
print(r.request.body)
r = requests.post('https://httpbin.org/post', json={'type': 'Run', 'calories': 100})
print(r.request.body)
Result
type=Run&calories=100
b'{"type": "Run", "calories": 100}'
I am trying to create a FB-chat like application. I have already started with the bare minimum of flask socketio implementation. So far, I already understand that client and server side communicate through a common handler. My current implementation is a free for all chat. for brevity i just show the handlers:
Client side:
socket.on('message', function(msg) {
$("#messages").append('<li>'+msg+'</li>');
console.log('Received message');
});
Server side:
#socketio.on('message')
def handleMessage(msg):
print('Message: ' + msg)
send(msg, broadcast=True)
Question:
How do I pass in arguments so I could have different chat ids
I guess what you want to do is some chat rooms. Good news! Flask-SocketIO is now natively dealing with rooms.
You can import the modules like this :
from flask_socketio import SocketIO, emit, join_room, leave_room,
close_room, rooms, disconnect
For joining a room, it looks like this :
#socketio.on('join', namespace='/test')
def join(message):
join_room(message['room'])
session['receive_count'] = session.get('receive_count', 0) + 1
emit('my_response',
{'data': 'In rooms: ' + ', '.join(rooms()),
'count': session['receive_count']})
You can find a complete example of code in the Miguel's Github page :)
Hi sorry for previous answer.
You can get cookies from sid. If you save chat id into cookies. you can identify user id easily.
this is my namespace code.
import socketio
import imp
import json
from datetime import datetime, timedelta
gameDBModule = imp.load_compiled("ChatDatabase", "./game_chat/chat_database.pyc")
apiModule = imp.load_compiled("TotoAPI", "./game_api/__init__.pyc")
class GameChat(socketio.Namespace):
def init(self):
self.lastDataIndex = 0
self.api = apiModule.TotoAPI()
def on_join(self, sid, message):
self.db = gameDBModule.ChatDatabase()
sessid = self.getUserId(sid)
try:
info = json.loads(self.api.userinfo(sessid))
userID = str(info[4])
except Exception as e:
userID = None
self.emit('join', {'history': self.db.get_chathistory(), 'channel': message, 'moderator': False, 'user_id': userID}, room=sid)
def on_connect(self, sid, environ):
self.emit('connect', {'data': 'Connected', 'count': 0}, room=sid)
print('GameChat Connected')
def on_say(self, sid, message, environ):
sessid =self.getUserId(sid)
try:
info = json.loads(self.api.userinfo(sessid))
userID = str(info[4])
except Exception as e:
userID = None
if userID !=None:
userID = None
#Save into History
now = self.get_current_time(9)
self.lastDataIndex = self.lastDataIndex + 1
item = {
'dataIndex': self.lastDataIndex,
'bot': False,
'date': now,
'message': message,
'role': 'user',
'type': 'say',
'user_id': userID
}
self.emit('msg', item)
item['date'] = self.get_current_time(0)
self.db.insert_chatting(item)
def on_disconnect(self, sid):
print('GameChat disconnected')
def getUserId(self, sid):
try:
#Get UserID from Cookies
cookies = str(self.server.environ[sid]['HTTP_COOKIE']).split(';')
id = None
for item in (cookies):
name=str(item).split('=')[0].strip()
value=str(item).split('=')[1].strip()
if name.__eq__('ci_session'):
id = value
return id
except Exception as e:
return None
def get_current_time(self, diff):
i = datetime.now() - timedelta(hours = diff)
return i.isoformat()[:-3]+"Z"
this is the code using above name space
async_mode = 'eventlet'
import eventlet
import eventlet.wsgi
from flask import Flask, render_template, Response, request, make_response
import socketio
import imp
gameChatModule = imp.load_compiled("GameChat", "./game_chat/__init__.pyc")
apiModule = imp.load_compiled("TotoAPI", "./game_api/__init__.pyc")
sio = socketio.Server(logger=False, async_mode=async_mode)
app = Flask(__name__)
app.wsgi_app = socketio.Middleware(sio, app.wsgi_app)
app.config['SECRET_KEY'] = 'secret!'
api = apiModule.TotoAPI()
ns = gameChatModule.GameChat("/")
ns.init()
if __name__ == '__main__':
sio.register_namespace(ns)
eventlet.wsgi.server(eventlet.listen(('', 8880)), app)
I have implemented it in python 2.7 You seem use python 3. but you can implement my code.
I'm a newbie in Flask and was wondering how to implement Flask API (http://www.flaskapi.org/) along with the main website, for example, main website (/), access API (/api/). In fact, this gives me an Internal Server Error.
Thank you.
from flask import Flask, request, url_for
from flask.ext.api import FlaskAPI, status, exceptions
# the all-important app variable:
app = Flask(__name__)
app2 = FlaskAPI(__name__)
#app.route("/")
def hello():
return "Oh, Hello Worldaa"
notes = {
0: 'do the shopping',
1: 'build the codez',
2: 'paint the door',
}
def note_repr(key):
return {
'url': request.host_url.rstrip('/api/') + url_for('notes_detail', key=key),
'text': notes[key]
}
#app2.route("/api/", methods=['GET', 'POST'])
def notes_list():
"""
List or create notes.
"""
if request.method == 'POST':
note = str(request.data.get('text', ''))
idx = max(notes.keys()) + 1
notes[idx] = note
return note_repr(idx), status.HTTP_201_CREATED
# request.method == 'GET'
return [note_repr(idx) for idx in sorted(notes.keys())]
#app2.route("/api/<int:key>/", methods=['GET', 'PUT', 'DELETE'])
def notes_detail(key):
"""
Retrieve, update or delete note instances.
"""
if request.method == 'PUT':
note = str(request.data.get('text', ''))
notes[key] = note
return note_repr(key)
elif request.method == 'DELETE':
notes.pop(key, None)
return '', status.HTTP_204_NO_CONTENT
# request.method == 'GET'
if key not in notes:
raise exceptions.NotFound()
return note_repr(key)
if __name__ == "__main__":
app.run(debug=True)
app2.run(debug=True)
You can look at Blueprint in the documentation of Flask.
For example:
main = Blueprint(__name__)
#main.route("/")
def index():
return "Hello, World."
api = Blurprint(__name__)
#api.route("/test")
def test():
return jsonify(dict(test="test")})
app = Flask(__name__)
app.register_blueprint(main)
app.register_blueprint(api, url_prefix="/api")
and then, you access index page by http://example.com, access api by http://example.com/api/test
I found here a nice template for a pagination. However, this example was done with SQLlite, but I rewrote in order to be able to use PyMongo.
This script creates users and save them to MongoDB with help of PyMongo:
import sys
from pymongo import MongoClient
def fill_data(users_no):
for i in range(users_no):
doc = {
'_id': str(i),
'uname': "name_" + str(i),
}
sDB.insert(doc)
if __name__ == '__main__':
db = MongoClient().test
sDB = db.users
fill_data(1000)
I modified the orginal app.py script in onrder to be able to use PyMongo. This is the modified script:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
from __future__ import unicode_literals
from flask import Flask, render_template, g, current_app, request
from flask.ext.paginate import Pagination
from pymongo import MongoClient
app = Flask(__name__)
app.config.from_pyfile('app.cfg')
#app.route('/')
def index():
db_name = "test"
col_name = "users"
db = MongoClient()
sDB = db[db_name][col_name]
total = sDB.find().count()
print total
page, per_page, offset = get_page_items()
users = sDB.find().skip(offset).limit(per_page)
for u in users:
print u
pagination = get_pagination(page=page,
per_page=per_page,
total=total,
record_name=users,
)
return render_template('index.html', users=users,
page=page,
per_page=per_page,
pagination=pagination,
)
def get_css_framework():
return current_app.config.get('CSS_FRAMEWORK', 'bootstrap3')
def get_link_size():
return current_app.config.get('LINK_SIZE', 'sm')
def show_single_page_or_not():
return current_app.config.get('SHOW_SINGLE_PAGE', False)
def get_page_items():
page = int(request.args.get('page', 1))
per_page = request.args.get('per_page')
if not per_page:
per_page = current_app.config.get('PER_PAGE', 10)
else:
per_page = int(per_page)
offset = (page - 1) * per_page
return page, per_page, offset
def get_pagination(**kwargs):
kwargs.setdefault('record_name', 'records')
return Pagination(css_framework=get_css_framework(),
link_size=get_link_size(),
show_single_page=show_single_page_or_not(),
**kwargs
)
if __name__ == '__main__':
app.run(debug=True)
Is there a way to avoid to use count() by clicking to the next or previous page in the pagination menu?
What did I forget to change so the actual user get be shown in the browser, because currently I only get the pagination menu without users?
User this link, It w'll help to paginate datas using flask-paginate
https://harishvc.com/2015/04/15/pagination-flask-mongodb/
if you are not using Flask use:
https://scalegrid.io/blog/fast-paging-with-mongodb/
Hope it will help !