Skip to content

Commit

Permalink
feat: add metadata about service and method to requesthandler
Browse files Browse the repository at this point in the history
Adding metadata about the service and method which is invoked
to the call of the requestHandler. This allows a request-handler
to resolve proper service and method configurations for http requests
if need be. An example usage is to use the default service configuration
in order to resolve retry- or hedging-policy for a call.

This change should be backwards compatible, since the request handler
signature specifies the meta as an optional argument.
  • Loading branch information
sfinnman authored and sfinnman committed Oct 11, 2022
1 parent 32deeab commit c97738f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ type RequestType = {
body: string | null;
};

type RequestHandler = (request: RequestType) => Promise<unknown>;
type RequestHandler = (request: RequestType, meta: { service: string, method: string }) => Promise<unknown>;

export function createFreightServiceClient(
handler: RequestHandler
Expand All @@ -447,6 +447,9 @@ export function createFreightServiceClient(
path: uri,
method: "GET",
body,
}, {
service: "FreightService",
method: "GetShipper",
}) as Promise<Shipper>;
},
ListShippers(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -467,6 +470,9 @@ export function createFreightServiceClient(
path: uri,
method: "GET",
body,
}, {
service: "FreightService",
method: "ListShippers",
}) as Promise<ListShippersResponse>;
},
CreateShipper(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -481,6 +487,9 @@ export function createFreightServiceClient(
path: uri,
method: "POST",
body,
}, {
service: "FreightService",
method: "CreateShipper",
}) as Promise<Shipper>;
},
UpdateShipper(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -501,6 +510,9 @@ export function createFreightServiceClient(
path: uri,
method: "PATCH",
body,
}, {
service: "FreightService",
method: "UpdateShipper",
}) as Promise<Shipper>;
},
DeleteShipper(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -518,6 +530,9 @@ export function createFreightServiceClient(
path: uri,
method: "DELETE",
body,
}, {
service: "FreightService",
method: "DeleteShipper",
}) as Promise<Shipper>;
},
GetSite(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -535,6 +550,9 @@ export function createFreightServiceClient(
path: uri,
method: "GET",
body,
}, {
service: "FreightService",
method: "GetSite",
}) as Promise<Site>;
},
ListSites(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -558,6 +576,9 @@ export function createFreightServiceClient(
path: uri,
method: "GET",
body,
}, {
service: "FreightService",
method: "ListSites",
}) as Promise<ListSitesResponse>;
},
CreateSite(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -575,6 +596,9 @@ export function createFreightServiceClient(
path: uri,
method: "POST",
body,
}, {
service: "FreightService",
method: "CreateSite",
}) as Promise<Site>;
},
UpdateSite(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -595,6 +619,9 @@ export function createFreightServiceClient(
path: uri,
method: "PATCH",
body,
}, {
service: "FreightService",
method: "UpdateSite",
}) as Promise<Site>;
},
DeleteSite(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -612,6 +639,9 @@ export function createFreightServiceClient(
path: uri,
method: "DELETE",
body,
}, {
service: "FreightService",
method: "DeleteSite",
}) as Promise<Site>;
},
GetShipment(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -629,6 +659,9 @@ export function createFreightServiceClient(
path: uri,
method: "GET",
body,
}, {
service: "FreightService",
method: "GetShipment",
}) as Promise<Shipment>;
},
ListShipments(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -652,6 +685,9 @@ export function createFreightServiceClient(
path: uri,
method: "GET",
body,
}, {
service: "FreightService",
method: "ListShipments",
}) as Promise<ListShipmentsResponse>;
},
CreateShipment(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -669,6 +705,9 @@ export function createFreightServiceClient(
path: uri,
method: "POST",
body,
}, {
service: "FreightService",
method: "CreateShipment",
}) as Promise<Shipment>;
},
UpdateShipment(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -689,6 +728,9 @@ export function createFreightServiceClient(
path: uri,
method: "PATCH",
body,
}, {
service: "FreightService",
method: "UpdateShipment",
}) as Promise<Shipment>;
},
DeleteShipment(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -706,6 +748,9 @@ export function createFreightServiceClient(
path: uri,
method: "DELETE",
body,
}, {
service: "FreightService",
method: "DeleteShipment",
}) as Promise<Shipment>;
},
};
Expand Down
20 changes: 19 additions & 1 deletion examples/proto/gen/typescript/einride/example/syntax/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ type RequestType = {
body: string | null;
};

type RequestHandler = (request: RequestType) => Promise<unknown>;
type RequestHandler = (request: RequestType, meta: { service: string, method: string }) => Promise<unknown>;

export function createSyntaxServiceClient(
handler: RequestHandler
Expand Down Expand Up @@ -333,6 +333,9 @@ export function createSyntaxServiceClient(
path: uri,
method: "GET",
body,
}, {
service: "SyntaxService",
method: "QueryOnly",
}) as Promise<Message>;
},
EmptyVerb(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -347,6 +350,9 @@ export function createSyntaxServiceClient(
path: uri,
method: "GET",
body,
}, {
service: "SyntaxService",
method: "EmptyVerb",
}) as Promise<wellKnownEmpty>;
},
StarBody(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -361,6 +367,9 @@ export function createSyntaxServiceClient(
path: uri,
method: "POST",
body,
}, {
service: "SyntaxService",
method: "StarBody",
}) as Promise<Message>;
},
Body(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -383,6 +392,9 @@ export function createSyntaxServiceClient(
path: uri,
method: "POST",
body,
}, {
service: "SyntaxService",
method: "Body",
}) as Promise<Message>;
},
Path(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -408,6 +420,9 @@ export function createSyntaxServiceClient(
path: uri,
method: "POST",
body,
}, {
service: "SyntaxService",
method: "Path",
}) as Promise<Message>;
},
PathBody(request) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand All @@ -430,6 +445,9 @@ export function createSyntaxServiceClient(
path: uri,
method: "POST",
body,
}, {
service: "SyntaxService",
method: "PathBody",
}) as Promise<Message>;
},
};
Expand Down
5 changes: 4 additions & 1 deletion internal/plugin/servicegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s serviceGenerator) generateHandler(f *codegen.File) {
f.P(t(1), "body: string | null;")
f.P("};")
f.P()
f.P("type RequestHandler = (request: RequestType) => Promise<unknown>;")
f.P("type RequestHandler = (request: RequestType, meta: { service: string, method: string }) => Promise<unknown>;")
f.P()
}

Expand Down Expand Up @@ -105,6 +105,9 @@ func (s serviceGenerator) generateMethod(f *codegen.File, method protoreflect.Me
f.P(t(4), "path: uri,")
f.P(t(4), "method: ", strconv.Quote(rule.Method), ",")
f.P(t(4), "body,")
f.P(t(3), "}, {")
f.P(t(4), "service: \"", method.Parent().Name(), "\",")
f.P(t(4), "method: \"", method.Name(), "\",")
f.P(t(3), "}) as Promise<", outputType.Reference(), ">;")
f.P(t(2), "},")
return nil
Expand Down

0 comments on commit c97738f

Please sign in to comment.