Skip to content

Instantly share code, notes, and snippets.

@dubeyji10
Last active July 25, 2022 07:49
Show Gist options
  • Save dubeyji10/ad526a42d36484057aae3ebdf669bcda to your computer and use it in GitHub Desktop.
Save dubeyji10/ad526a42d36484057aae3ebdf669bcda to your computer and use it in GitHub Desktop.
Server side Testing of api requests
from urllib import response
from flask import Flask, request, render_template, session, redirect , Response
import json
from bson import json_util, ObjectId
import pprint
import json
import requests
import time
#mongodb functions sample
app = Flask(__name__)
app.debug = True
@app.route('/')
def index():
# make request to http://localhost:3000/api/getAll
# making request
response = requests.get(url='http://localhost:3000/api/getAll')
jsonData = json.loads(response.text)
# print('response json : ',jsonData)
print("\n\n\n returned first 50 records \n\n\n")
return render_template('layouts/index.html',
fields=[],
data = jsonData['message']
)
@app.route('/all')
def all_records():
'''
return all records
'''
allDataJsons = []
# find with empty filter -- all documents in collections returned
return render_template('layouts/allRecords.html',
data = allDataJsons
)
@app.route('/sample')
def fun1():
return "<h1> hi! , just a message</h1>"
def getDistinct():
response = requests.get(url='http://localhost:3000/api/getDistinct')
# print('response : ',response.text)
return json.loads(response.text)
@app.route('/distinct')
def distinctItemsFunc():
distinctItems = getDistinct()
# print('distinctItems.message : ',distinctItems['message'])
return render_template('layouts/distinctFields.html',
data = distinctItems['message']
)
@app.route('/sample6/')
@app.route('/sample6')
def fun6_2():
# jsonList = generate_data(number)
return "<h1> enter a number in path to render table of data like /sample6/50 </h1>"
@app.route('/owners/')
def fun6():
payloadTest = {
'owner':['BOVE Giorgio dit Georges',
'CHETCUTI Emmanuel', 'CHETCUTI Nadia',
'COELHO GRACA FRANCISCO BENTO Didia Violinda',
'COLARUSSO Gerardo Martino', 'COLARUSSO Lina',
'CONFEDERATION SUISSE', 'DAME Giel Ine',
'DAVEL Gisela Inge', 'DEGRAS Rahman'
]
}
print('a post request to server')
response = requests.post('http://localhost:3000/api/condition-database-test',data=payloadTest)
# print('------------------\n',response.text,'\n---------------------')
jsonRes = json.loads(response.text)
print(jsonRes,'\n\n')
return render_template('layouts/template2.html',
data=jsonRes['message'],
query = jsonRes['query'])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9999)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment