responser
1.0.0
Responser เป็นแพ็คเกจ Python เพื่อแปลงสตริงวัตถุและข้อมูลอื่น ๆ เพื่อ Rest API Response Convention และในรูปแบบ JSON
ResponserResponser เป็นแพ็คเกจ Python เพื่อแปลงสตริงวัตถุและข้อมูลอื่น ๆ เพื่อ Rest API Response Convention และในรูปแบบ JSON
ฟังก์ชั่นนี้เป็นไปตามโครงสร้าง JSON ต่อไปนี้
{
"status_code" : 200 ,
"data" : {
"name" : " Bharath Kumar Ravichandran " ,
"alma_mater" : " NIT Trichy " ,
"languages_known" : [
" Python " ,
" PHP " ,
" JS " ,
" C++ "
],
"cool_guy" : " yes " ,
},
"message" : " OK "
}JSONResponserfrom responser import JSONResponserJSONResponser(status_code=400, data=None, message=None, strict_mode=false)status_code สามารถเป็นรหัสสถานะ http หรือรหัสสถานะที่กำหนดเองของคุณเองstatus_code เป็นรหัสสถานะ HTTP และ data คือ None วลีเหตุผลเริ่มต้นจะถูกเพิ่ม (ถ้า strict_mode เป็น False (ค่าเริ่มต้น))status_code เป็นรหัสสถานะ HTTP และ message คือ None วลีเหตุผลเริ่มต้นจะถูกเพิ่ม (แม้ว่า strict_mode จะเป็น True (ค่าเริ่มต้น))status_code ไม่ใช่รหัสสถานะ HTTP และ data คือ None ข้อมูลที่ว่างจะถูกเพิ่มstatus_code ไม่ใช่รหัสสถานะ HTTP และ message คือ None ข้อความว่างจะถูกเพิ่มstrict_mode เป็น True ข้อมูลที่ให้เป็น data จะถูกเข้ารหัสstatus_code เป็น 400รหัสตัวอย่าง
from responser import JSONResponser
status_code = 200
data = {
"name" : "Bharath Kumar Ravichandran" ,
"alma_mater" : "NIT Trichy" ,
"languages_known" : [
"Python" ,
"PHP" ,
"JS" ,
"C++"
],
"cool_guy" : "yes" ,
}
message = "User details returned."
response = JSONResponser ( status_code , data , message )
print responseเอาท์พุท
{
"status_code" : 200 ,
"data" : {
"name" : " Bharath Kumar Ravichandran " ,
"alma_mater" : " NIT Trichy " ,
"languages_known" : [
" Python " ,
" PHP " ,
" JS " ,
" C++ "
],
"cool_guy" : " yes " ,
},
"message" : " User details returned. "
}JSONResponserDecoratorfrom responser import JSONResponserDecorator@JSONResponserDecoratorJSONResponserDecorator สร้างขึ้นบน JsonResponser ดังนั้นจึงเป็นไปตามการประชุมเช่นเดียวกับ JSONResponserรหัสตัวอย่าง
from responser import JSONResponserDecorator
@ JSONResponserDecorator
def sample_function ():
data = {
"name" : "Bharath Kumar Ravichandran" ,
"alma_mater" : "NIT Trichy" ,
"languages_known" : [
"Python" ,
"PHP" ,
"JS" ,
"C++"
],
"cool_guy" : "yes" ,
}
return dataข้อมูลที่ส่งคืน
{
"status_code" : 200 ,
"data" : {
"name" : " Bharath Kumar Ravichandran " ,
"alma_mater" : " NIT Trichy " ,
"languages_known" : [
" Python " ,
" PHP " ,
" JS " ,
" C++ "
],
"cool_guy" : " yes " ,
},
"message" : " OK "
}Gokulsrinivas/Sangria
Bharath Kumar Ravichandran
มิกซ์