service Usermanagement {
rpc UserLogin (UserRequest) returns (UserResponse) {}
}
// The request message containing the user's name.
message UserRequest {
string username = 1;
string password = 2;
}
// The response message containing the greetings
message UserResponse {
string message = 1;
string fullname = 2;
string token = 4;
int32 status = 5;
}
import grpc
from userservice import userservices_pb2
from userservice import userservices_pb2_grpc
from usermanagement.models import AppUser as User
from django.contrib.auth import authenticate
from rest_framework.authtoken.models import Token
from google.protobuf import any_pb2
class Usermanagement(userservices_pb2_grpc.UsermanagementServicer):
def UserLogin(self, request, context):
User = authenticate(username=request.username, password=request.password)
if User is not None:
(token, created) = Token.objects.get_or_create(user=User)
User.authentication_token = token.key
return userservices_pb2.UserResponse(message='success', status=0, fullname=User.fullname, token = token.key)
else:
return userservices_pb2.UserResponse(message='Incorrect Username/Password', status=1)
message UserResponse {
string message = 1;
string fullname = 2;
string token = 4;
int32 status = 5;
}
message UserListRequest {
}
message UserListResponse {
message Result {
string fullname = 1;
}
repeated Result results = 1;
}
def UserList(self, request, context):
UserListRes = User.objects.all().values('fullname')
return userservices_pb2.UserListResponse(results=UserListRes)