Professional Documents
Culture Documents
Rest Api Class 04 345678
Rest Api Class 04 345678
test.py
import requests
BASE_URL='http://127.0.0.1:8000/'
ENDPOINT='api/'
def get_resource1():
resp=requests.get(BASE_URL+ENDPOINT)
#http://127.0.0.1:8000/api/id/
print(resp.status_code)
print(resp.json())
def get_resource2(id):
resp=requests.get(BASE_URL+ENDPOINT+id+'/')
print(resp.status_code)
print(resp.json())
get_resource1()
if ch==2:
id=input("Enter id")
get_resource2(id)
views.py
import json
class EmployeeDetails(SerializeMixin,View):
def get(self,request,id,*args,**kwargs):
emp=Employee.objects.get(id=id)
json_data=self.fun([emp])
return HttpResponse(json_data,content_type='application/json')
class EmployeeList(SerializeMixin,View):
def get(self,request,*args,**kwargs):
qs=Employee.objects.all()
json_data=self.fun(qs)
return HttpResponse(json_data,content_type='application/json')
myApp/mixins.py
import json
class SerializeMixin(object):
def fun(self,qs):
json_data=serialize('json',qs)
p_dict=json.loads(json_data)
result=[]
for ob in p_dict:
emp_data=ob['fields']
result.append(emp_data)
json_data=json.dumps(result)
return json_data
urls.py
urlpatterns = [
url('admin/', admin.site.urls),
url('api/$',EmployeeList.as_view()),
url('api/(?P<id>\d+)/$',EmployeeDetails.as_view())
Exception Handling
views.py
import json
class EmployeeDetails(SerializeMixin,View):
def get(self,request,id,*args,**kwargs):
try:
emp=Employee.objects.get(id=id)
except:
else:
json_data=self.fun([emp])
return HttpResponse(json_data,content_type='application/json')
class EmployeeList(SerializeMixin,View):
def get(self,request,*args,**kwargs):
qs=Employee.objects.all()
json_data=self.fun(qs)
return HttpResponse(json_data,content_type='application/json')
Assuming that our views.py does not handle the exception,then we can also
handle the exception as shown below:
import requests
BASE_URL='http://127.0.0.1:8000/'
ENDPOINT='api/'
def get_resource1():
resp=requests.get(BASE_URL+ENDPOINT)
#http://127.0.0.1:8000/api/id/
print(resp.status_code)
print(resp.json())
def get_resource2(id):
resp=requests.get(BASE_URL+ENDPOINT+id+'/')
else:
print(resp.json())
print("Enter 1-->to display all records 2-->to display a particular record")
if ch==1:
get_resource1()
if ch==2:
id=input("Enter id")
get_resource2(id)
test.py
import requests
BASE_URL='http://127.0.0.1:8000/'
ENDPOINT='api/'
def get_resource1():
resp=requests.get(BASE_URL+ENDPOINT)
#http://127.0.0.1:8000/api/id/
print(resp.status_code)
print(resp.json())
def get_resource2(id):
resp=requests.get(BASE_URL+ENDPOINT+id+'/')
print(resp.json())
print(resp.status_code)
print("Enter 1-->to display all records 2-->to display a particular record")
if ch==1:
get_resource1()
if ch==2:
id=input("Enter id")
get_resource2(id)
mixins.py
import json
class HttpResponseMixin(object):
return
HttpResponse(json_data,content_type='application/json',status=status)
class SerializeMixin(object):
def fun(self,qs):
json_data=serialize('json',qs)
p_dict=json.loads(json_data)
result=[]
for ob in p_dict:
emp_data=ob['fields']
result.append(emp_data)
json_data=json.dumps(result)
return json_data
views.py
import json
class EmployeeDetails(SerializeMixin,HttpResponseMixin,View):
def get(self,request,id,*args,**kwargs):
try:
emp=Employee.objects.get(id=id)
except:
return self.render_to_http_response(json_data,status=404)
else:
json_data=self.fun([emp])
return self.render_to_http_response(json_data)
class EmployeeList(SerializeMixin,View):
def get(self,request,*args,**kwargs):
qs=Employee.objects.all()
json_data=self.fun(qs)
return HttpResponse(json_data,content_type='application/json')
urls.py
urlpatterns = [
url('admin/', admin.site.urls),
url('api/$',EmployeeList.as_view()),
url('api/(?P<id>\d+)/$',EmployeeDetails.as_view())