|
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,78 @@ 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 | + const experimentsClient = getExperimentsClient(); |
| 144 | + // TODO(ak) remove after migration |
| 145 | + async function resolveClient(): Promise<PromiseClient<T>> { |
| 146 | + if (!jsonRpcClient) { |
| 147 | + return grpcClient; |
| 148 | + } |
| 149 | + // TODO(ak): is not going to work for getLoggedInUser itself |
| 150 | + const [isPublicAPIEnabled, isFgaChecksEnabled] = await Promise.all([ |
| 151 | + experimentsClient.getValueAsync("dashboard_public_api_enabled", false, { |
| 152 | + user, |
| 153 | + gitpodHost: window.location.host, |
| 154 | + }), |
| 155 | + experimentsClient.getValueAsync("centralizedPermissions", false, { |
| 156 | + user, |
| 157 | + gitpodHost: window.location.host, |
| 158 | + }), |
| 159 | + ]); |
| 160 | + if (isPublicAPIEnabled && isFgaChecksEnabled) { |
| 161 | + return grpcClient; |
| 162 | + } |
| 163 | + return jsonRpcClient; |
| 164 | + } |
| 165 | + /** |
| 166 | + * The original application error is retained using gRPC metadata to ensure that existing error handling remains intact. |
| 167 | + */ |
| 168 | + function handleError(e: any): unknown { |
| 169 | + if (e instanceof ConnectError) { |
| 170 | + throw converter.fromError(e); |
| 171 | + } |
| 172 | + throw e; |
| 173 | + } |
| 174 | + return (...args: any[]) => { |
| 175 | + const method = type.methods[prop as string]; |
| 176 | + if (!method) { |
| 177 | + throw new ConnectError("unimplemented", Code.Unimplemented); |
| 178 | + } |
| 179 | + |
| 180 | + // TODO(ak) default timeouts |
| 181 | + // TODO(ak) retry on unavailable? |
| 182 | + |
| 183 | + if (method.kind === MethodKind.Unary || method.kind === MethodKind.ClientStreaming) { |
| 184 | + return (async () => { |
| 185 | + try { |
| 186 | + const client = await resolveClient(); |
| 187 | + const result = await Reflect.apply(client[prop as any], client, args); |
| 188 | + return result; |
| 189 | + } catch (e) { |
| 190 | + handleError(e); |
| 191 | + } |
| 192 | + })(); |
| 193 | + } |
| 194 | + return (async function* () { |
| 195 | + try { |
| 196 | + const client = await resolveClient(); |
| 197 | + const generator = Reflect.apply(client[prop as any], client, args) as AsyncGenerator<any>; |
| 198 | + for await (const item of generator) { |
| 199 | + yield item; |
| 200 | + } |
| 201 | + } catch (e) { |
| 202 | + handleError(e); |
| 203 | + } |
| 204 | + })(); |
| 205 | + }; |
| 206 | + }, |
| 207 | + }); |
| 208 | +} |
0 commit comments