This article is for the 21st day of Volare Advent Calendar 2019.
This time I made the API of iOS application for the first time. Partly because I'm studying, I tried to create an API using Django REST Framework, so I'd like to write about it: relaxed: The article on the iOS side of the created application is here
The main target for creating apps is college students living alone like us: dark_sunglasses: For details on how the app was decided, It's a hassle to think about menus when cooking for yourself, but existing apps search by looking at what's in the refrigerator. → The target of existing ones is for people with families
I like it. I found it convenient to have an app that automatically presents menus by entering dislikes! → Basically, I don't make much, I want to make what I like at any time (the refrigerator doesn't have much ingredients: ghost :)
I decided to make it from such a point! (If you don't have the same concept, let's make one: relaxed :)
Team development together, Ryu-chan explains the app. This time I was in charge of this backend.
On the backend, we have implemented the following 5 features!
I was in charge of three of them, so I will write about that part!
from main.models import User, FoodConfigParam
from main.serializers import AuthSerializer
from rest_framework.generics import GenericAPIView
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.response import Response
import random
from rest_framework_jwt.settings import api_settings
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
class RegisterAuthView(GenericAPIView):
permission_classes = ()
serializer_class = AuthSerializer
def post(self, request):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
#Returns token if uuid is registered
if User.objects.filter(uuid=serializer.data['uuid']):
user = User.objects.get(uuid=serializer.data['uuid'])
payload = jwt_payload_handler(user)
return Response({
'token': jwt_encode_handler(payload),
})
user = User.objects.create_user(uuid=serializer.data['uuid'])
user.save()
if not user:
raise AuthenticationFailed()
payload = jwt_payload_handler(user)
#Add default ingredients when registering as a user
user = User.objects.filter(uuid=serializer.data['uuid']).last()
food = FoodConfigParam()
food.create_defaultfood(user=user)
return Response({
'token': jwt_encode_handler(payload),
})
Check if there is a user in the DB that matches the UUID sent, and if not, create a user.
from main.models import FoodConfigParam
from main.serializers import FoodConfigParamSerializer
from rest_framework.generics import GenericAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
class UserFoodConfig(GenericAPIView):
queryset = FoodConfigParam.objects.all()
serializer_class = FoodConfigParamSerializer
def get(self, request):
foodConfigParams = FoodConfigParam.objects.filter(user=request.user)
serializer = FoodConfigParamSerializer(foodConfigParams, many=True)
return Response({'data': serializer.data})
def put(self, request):
res = []
for config in request.data['data']:
name = config['name']
rate = config['rate']
#Extract the ones that match the registered user and the ingredient name
foodConfigParam = FoodConfigParam.objects.get(user=request.user, name=name)
#Change probability
serializer = FoodConfigParamSerializer(foodConfigParam, data={'rate': rate}, partial=True)
serializer.is_valid(raise_exception=True)
serializer.save()
res.append(serializer.data)
return Response({'data': res})
The ones that match the user and the ingredient name are extracted, and the appearance rate is changed.
The API I made did not work well, or it worked but it worked unexpectedly when connected to iOS. On the iOS side, it seems that there are various things such as trying not to write logic as much as possible and reducing the number of communications, I felt that if I didn't have more knowledge on the front side, I wouldn't be able to communicate and it would be difficult ... I consulted with the iOS side and proceeded while discussing how easy it would be to do this, and I learned.
This time I was making something that works for the time being, so I still don't understand RESTful: cry: So, I knew the goodness of django-rest-framework and decided to study so that I could make the most of it. Thank you for reading to the end.
Recommended Posts