blob: 8e85a6610139d567b5cf2ae8ef9b0f829f88ed60 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2020 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Copybara854996b2021-09-07 19:36:02 +00004
5from __future__ import print_function
6from __future__ import division
7from __future__ import absolute_import
8
9from google.protobuf import empty_pb2
10
11from api import resource_name_converters as rnc
12from api.v3 import monorail_servicer
13from api.v3 import api_constants
14from api.v3.api_proto import users_pb2
15from api.v3.api_proto import user_objects_pb2
16from api.v3.api_proto import users_prpc_pb2
17from businesslogic import work_env
18from framework import exceptions
19
20
21class UsersServicer(monorail_servicer.MonorailServicer):
22 """Handle API requests related to User objects.
23 Each API request is implemented with a method as defined in the
24 .proto file. Each method does any request-specific validation, uses work_env
25 to safely operate on business objects, and returns a response proto.
26 """
27
28 DESCRIPTION = users_prpc_pb2.UsersServiceDescription
29
30 @monorail_servicer.PRPCMethod
31 def GetUser(self, mc, request):
32 # type: (MonorailContext, GetUserRequest) ->
33 # GetUserResponse
34 """pRPC API method that implements GetUser.
35
36 Raises:
37 InputException if a name in request.name is invalid.
38 NoSuchUserException if a User is not found.
39 """
40 user_id = rnc.IngestUserName(mc.cnxn, request.name, self.services)
41
42 with work_env.WorkEnv(mc, self.services) as we:
43 user = we.GetUser(user_id)
44
45 return self.converter.ConvertUser(user)
46
47 @monorail_servicer.PRPCMethod
48 def BatchGetUsers(self, mc, request):
49 # type: (MonorailContext, BatchGetUsersRequest) ->
50 # BatchGetUsersResponse
51 """pRPC API method that implements BatchGetUsers.
52
53 Raises:
54 InputException if a name in request.names is invalid.
55 NoSuchUserException if a User is not found.
56 """
57 if len(request.names) > api_constants.MAX_BATCH_USERS:
58 raise exceptions.InputException(
59 'Requesting %d users when the allowed maximum is %d users.' %
60 (len(request.names), api_constants.MAX_BATCH_USERS))
61 user_ids = rnc.IngestUserNames(mc.cnxn, request.names, self.services)
62
63 with work_env.WorkEnv(mc, self.services) as we:
64 users = we.BatchGetUsers(user_ids)
65
66 api_users_by_id = self.converter.ConvertUsers(
67 [user.user_id for user in users])
68 api_users = [api_users_by_id[user_id] for user_id in user_ids]
69
70 return users_pb2.BatchGetUsersResponse(users=api_users)
71
72 @monorail_servicer.PRPCMethod
73 def StarProject(self, mc, request):
74 # type: (MonorailContext, StarProjectRequest) ->
75 # ProjectStar
76 """pRPC API method that implements StarProject.
77
78 Raises:
79 InputException if the project name in request.project is invalid.
80 NoSuchProjectException if no project exists with the given name.
81 """
82 project_id = rnc.IngestProjectName(mc.cnxn, request.project, self.services)
83
84 with work_env.WorkEnv(mc, self.services) as we:
85 we.StarProject(project_id, True)
86
87 user_id = mc.auth.user_id
88 star_name = rnc.ConvertProjectStarName(
89 mc.cnxn, user_id, project_id, self.services)
90
91 return user_objects_pb2.ProjectStar(name=star_name)
92
93 @monorail_servicer.PRPCMethod
94 def UnStarProject(self, mc, request):
95 # type: (MonorailContext, UnStarProjectRequest) ->
96 # Empty
97 """pRPC API method that implements UnStarProject.
98
99 Raises:
100 InputException if the project name in request.project is invalid.
101 NoSuchProjectException if no project exists with the given name.
102 """
103 project_id = rnc.IngestProjectName(mc.cnxn, request.project, self.services)
104
105 with work_env.WorkEnv(mc, self.services) as we:
106 we.StarProject(project_id, False)
107
108 return empty_pb2.Empty()
109
110 @monorail_servicer.PRPCMethod
111 def ListProjectStars(self, mc, request):
112 # type: (MonorailContext, ListProjectStarsRequest) ->
113 # ListProjectStarsResponse
114 """pRPC API method that implements ListProjectStars.
115
116 Raises:
117 InputException: if the `page_token` or `parent` is invalid.
118 NoSuchUserException: if the User is not found.
119 """
120 user_id = rnc.IngestUserName(mc.cnxn, request.parent, self.services)
121
122 with work_env.WorkEnv(mc, self.services) as we:
123 projects = we.ListStarredProjects(user_id)
124
125 # TODO(crbug.com/monorail/7175): Add pagination logic.
126 return users_pb2.ListProjectStarsResponse(
127 project_stars=self.converter.ConvertProjectStars(user_id, projects))