|
4 | 4 | * See License.AGPL.txt in the project root for license information. |
5 | 5 | */ |
6 | 6 |
|
7 | | -import { createPromiseClient } from "@connectrpc/connect"; |
| 7 | +import { Code, ConnectError, PromiseClient, createPromiseClient } from "@connectrpc/connect"; |
8 | 8 | import { createConnectTransport } from "@connectrpc/connect-web"; |
| 9 | +import { MethodKind, ServiceType } from "@bufbuild/protobuf"; |
| 10 | +import { TeamMemberInfo, TeamMemberRole, User } from "@gitpod/gitpod-protocol"; |
| 11 | +import { PublicAPIConverter } from "@gitpod/gitpod-protocol/lib/public-api-converter"; |
9 | 12 | import { Project as ProtocolProject, Team as ProtocolTeam } from "@gitpod/gitpod-protocol/lib/teams-projects-protocol"; |
10 | 13 | import { HelloService } from "@gitpod/public-api/lib/gitpod/experimental/v1/dummy_connect"; |
| 14 | +import { OIDCService } from "@gitpod/public-api/lib/gitpod/experimental/v1/oidc_connect"; |
| 15 | +import { ProjectsService } from "@gitpod/public-api/lib/gitpod/experimental/v1/projects_connect"; |
| 16 | +import { Project } from "@gitpod/public-api/lib/gitpod/experimental/v1/projects_pb"; |
11 | 17 | import { TeamsService } from "@gitpod/public-api/lib/gitpod/experimental/v1/teams_connect"; |
| 18 | +import { Team, TeamMember, TeamRole } from "@gitpod/public-api/lib/gitpod/experimental/v1/teams_pb"; |
12 | 19 | import { TokensService } from "@gitpod/public-api/lib/gitpod/experimental/v1/tokens_connect"; |
13 | | -import { ProjectsService } from "@gitpod/public-api/lib/gitpod/experimental/v1/projects_connect"; |
14 | | -import { WorkspacesService } from "@gitpod/public-api/lib/gitpod/experimental/v1/workspaces_connect"; |
15 | | -import { OIDCService } from "@gitpod/public-api/lib/gitpod/experimental/v1/oidc_connect"; |
| 20 | +import { WorkspacesService as WorkspaceV1Service } from "@gitpod/public-api/lib/gitpod/experimental/v1/workspaces_connect"; |
| 21 | +import { WorkspaceService } from "@gitpod/public-api/lib/gitpod/experimental/v2/workspace_connect"; |
16 | 22 | import { getMetricsInterceptor } from "@gitpod/public-api/lib/metrics"; |
17 | | -import { Team } from "@gitpod/public-api/lib/gitpod/experimental/v1/teams_pb"; |
18 | | -import { TeamMemberInfo, TeamMemberRole } from "@gitpod/gitpod-protocol"; |
19 | | -import { TeamMember, TeamRole } from "@gitpod/public-api/lib/gitpod/experimental/v1/teams_pb"; |
20 | | -import { Project } from "@gitpod/public-api/lib/gitpod/experimental/v1/projects_pb"; |
| 23 | +import { getExperimentsClient } from "../experiments/client"; |
| 24 | +import { JsonRpcWorkspaceClient } from "./json-rpc-workspace-client"; |
21 | 25 |
|
22 | 26 | const transport = createConnectTransport({ |
23 | 27 | baseUrl: `${window.location.protocol}//${window.location.host}/public-api`, |
24 | 28 | interceptors: [getMetricsInterceptor()], |
25 | 29 | }); |
26 | 30 |
|
| 31 | +export const converter = new PublicAPIConverter(); |
| 32 | + |
27 | 33 | export const helloService = createPromiseClient(HelloService, transport); |
28 | 34 | export const teamsService = createPromiseClient(TeamsService, transport); |
29 | 35 | export const personalAccessTokensService = createPromiseClient(TokensService, transport); |
30 | 36 | export const projectsService = createPromiseClient(ProjectsService, transport); |
31 | | -export const workspacesService = createPromiseClient(WorkspacesService, transport); |
| 37 | +/** |
| 38 | + * @deprecated use workspaceClient instead |
| 39 | + */ |
| 40 | +export const workspacesService = createPromiseClient(WorkspaceV1Service, transport); |
32 | 41 | export const oidcService = createPromiseClient(OIDCService, transport); |
33 | 42 |
|
| 43 | +export const workspaceClient = createServiceClient(WorkspaceService, new JsonRpcWorkspaceClient()); |
| 44 | + |
34 | 45 | export function publicApiTeamToProtocol(team: Team): ProtocolTeam { |
35 | 46 | return { |
36 | 47 | id: team.id, |
@@ -120,3 +131,70 @@ export function projectToProtocol(project: Project): ProtocolProject { |
120 | 131 | }, |
121 | 132 | }; |
122 | 133 | } |
| 134 | + |
| 135 | +let user: User | undefined; |
| 136 | +export function updateUser(newUser: User | undefined) { |
| 137 | + user = newUser; |
| 138 | +} |
| 139 | + |
| 140 | +function createServiceClient<T extends ServiceType>(type: T, jsonRpcClient?: PromiseClient<T>): PromiseClient<T> { |
| 141 | + return new Proxy(createPromiseClient(type, transport), { |
| 142 | + get(grpcClient, prop) { |
| 143 | + // TODO(ak) remove after migration |
| 144 | + async function resolveClient(): Promise<PromiseClient<T>> { |
| 145 | + if (!jsonRpcClient) { |
| 146 | + return grpcClient; |
| 147 | + } |
| 148 | + const isEnabled = await getExperimentsClient().getValueAsync("dashboard_public_api_enabled", false, { |
| 149 | + // TODO(ak): is not going to work for getLoggedInUser itself |
| 150 | + user, |
| 151 | + }); |
| 152 | + if (isEnabled) { |
| 153 | + return grpcClient; |
| 154 | + } |
| 155 | + return jsonRpcClient; |
| 156 | + } |
| 157 | + /** |
| 158 | + * The original application error is retained using gRPC metadata to ensure that existing error handling remains intact. |
| 159 | + */ |
| 160 | + function handleError(e: any): unknown { |
| 161 | + if (e instanceof ConnectError) { |
| 162 | + throw converter.fromError(e); |
| 163 | + } |
| 164 | + throw e; |
| 165 | + } |
| 166 | + return (...args: any[]) => { |
| 167 | + const method = type.methods[prop as string]; |
| 168 | + if (!method) { |
| 169 | + throw new ConnectError("unimplemented", Code.Unimplemented); |
| 170 | + } |
| 171 | + |
| 172 | + // TODO(ak) default timeouts |
| 173 | + // TODO(ak) retry on unavailable? |
| 174 | + |
| 175 | + if (method.kind === MethodKind.Unary || method.kind === MethodKind.ClientStreaming) { |
| 176 | + return (async () => { |
| 177 | + try { |
| 178 | + const client = await resolveClient(); |
| 179 | + const result = await Reflect.apply(client[prop as any], client, args); |
| 180 | + return result; |
| 181 | + } catch (e) { |
| 182 | + handleError(e); |
| 183 | + } |
| 184 | + })(); |
| 185 | + } |
| 186 | + return (async function* () { |
| 187 | + try { |
| 188 | + const client = await resolveClient(); |
| 189 | + const generator = Reflect.apply(client[prop as any], client, args) as AsyncGenerator<any>; |
| 190 | + for await (const item of generator) { |
| 191 | + yield item; |
| 192 | + } |
| 193 | + } catch (e) { |
| 194 | + handleError(e); |
| 195 | + } |
| 196 | + })(); |
| 197 | + }; |
| 198 | + }, |
| 199 | + }); |
| 200 | +} |
0 commit comments