MCPcopy Create free account
hub / github.com/Bytez-com/docs / Client

Class Client

sdk/python/bytez/client.py:6–59  ·  view source on GitHub ↗

Client class for handling HTTP requests to the Bytez API. Attributes: auth (dict): Authorization headers containing API key.

Source from the content-addressed store, hash-verified

4Response = namedtuple('Response', ['output', 'error', 'provider'], defaults=[None, None, None])
5
6class Client:
7 """
8 Client class for handling HTTP requests to the Bytez API.
9
10 Attributes:
11 auth (dict): Authorization headers containing API key.
12 """
13
14 def __init__( self, api_key, dev = False ):
15 """
16 Initialize the client with an API key.
17
18 Args:
19 api_key (str): Your Bytez API key.
20 """
21 self.headers = {
22 "lang": "python",
23 "authorization": f"Key {api_key}",
24 "content-type": "application/json",
25 }
26 self.host = f"http{'://' if dev else 's://'}{'localhost:8080' if dev else 'api.bytez.com'}/models/v2/"
27 def request(self, path = "", method = "GET", post_body = None, provider_key = None):
28 """
29 Send an HTTP request.
30
31 Args:
32 path (str): API endpoint path.
33 method (str): HTTP method (default: "GET").
34 body (dict, optional): Request body (default: None).
35
36 Returns:
37 dict | iter: JSON response or stream iterator.
38 """
39 try:
40 stream = bool(post_body and post_body.get("stream"))
41 response = requests.request(
42 method,
43 self.host + path,
44 headers = ( { **self.headers, "provider-key": provider_key } if provider_key is not None else self.headers ),
45 # drop null values from being sent
46 data = json.dumps({k: v for k, v in post_body.items() if v is not None}) if post_body else None,
47 stream = stream
48 )
49
50 if stream:
51 response.encoding = "utf-8"
52
53 return (line for line in response.iter_lines(decode_unicode=True) if line)
54 else:
55 results = response.json()
56
57 return Response(output=results.get('output'), error=results.get('error'), provider=results.get('provider'))
58 except Exception as error:
59 return Response(error=str(error))

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected