11from datetime import datetime
2- from typing import Optional
2+ from typing import Mapping , Optional , Protocol , Union
33
44import grpc
5- import httpx
65
76from dispatch .sdk .v1 import function_pb2 as function_pb
87from dispatch .sdk .v1 import function_pb2_grpc as function_grpc
1211 Request ,
1312 sign_request ,
1413)
14+ from dispatch .test .http import HttpClient
1515
1616
1717class EndpointClient :
@@ -24,15 +24,15 @@ class EndpointClient:
2424 """
2525
2626 def __init__ (
27- self , http_client : httpx . Client , signing_key : Optional [Ed25519PrivateKey ] = None
27+ self , http_client : HttpClient , signing_key : Optional [Ed25519PrivateKey ] = None
2828 ):
2929 """Initialize the client.
3030
3131 Args:
3232 http_client: Client to use to make HTTP requests.
3333 signing_key: Optional Ed25519 private key to use to sign requests.
3434 """
35- channel = _HttpxGrpcChannel (http_client , signing_key = signing_key )
35+ channel = _HttpGrpcChannel (http_client , signing_key = signing_key )
3636 self ._stub = function_grpc .FunctionServiceStub (channel )
3737
3838 def run (self , request : function_pb .RunRequest ) -> function_pb .RunResponse :
@@ -46,16 +46,10 @@ def run(self, request: function_pb.RunRequest) -> function_pb.RunResponse:
4646 """
4747 return self ._stub .Run (request )
4848
49- @classmethod
50- def from_url (cls , url : str , signing_key : Optional [Ed25519PrivateKey ] = None ):
51- """Returns an EndpointClient for a Dispatch endpoint URL."""
52- http_client = httpx .Client (base_url = url )
53- return EndpointClient (http_client , signing_key )
5449
55-
56- class _HttpxGrpcChannel (grpc .Channel ):
50+ class _HttpGrpcChannel (grpc .Channel ):
5751 def __init__ (
58- self , http_client : httpx . Client , signing_key : Optional [Ed25519PrivateKey ] = None
52+ self , http_client : HttpClient , signing_key : Optional [Ed25519PrivateKey ] = None
5953 ):
6054 self .http_client = http_client
6155 self .signing_key = signing_key
@@ -120,9 +114,11 @@ def __call__(
120114 wait_for_ready = None ,
121115 compression = None ,
122116 ):
117+ url = self .client .url_for (self .method ) # note: method==path in gRPC parlance
118+
123119 request = Request (
124120 method = "POST" ,
125- url = str ( httpx . URL ( self . client . base_url ). join ( self . method )) ,
121+ url = url ,
126122 body = self .request_serializer (request ),
127123 headers = CaseInsensitiveDict ({"Content-Type" : "application/grpc+proto" }),
128124 )
@@ -131,10 +127,10 @@ def __call__(
131127 sign_request (request , self .signing_key , datetime .now ())
132128
133129 response = self .client .post (
134- request .url , content = request .body , headers = request .headers
130+ request .url , body = request .body , headers = request .headers
135131 )
136132 response .raise_for_status ()
137- return self .response_deserializer (response .content )
133+ return self .response_deserializer (response .body )
138134
139135 def with_call (
140136 self ,
0 commit comments