Skip to content

Commit

Permalink
core design
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Dec 2, 2024
1 parent 646d455 commit d61ea2c
Show file tree
Hide file tree
Showing 80 changed files with 1,635 additions and 1,018 deletions.
8 changes: 7 additions & 1 deletion commune/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def determine_type(x):
except ValueError:
pass
return x


def forward(argv = None,
sep = '--',
fn_splitters = [':', '/', '//', '::'],
Expand All @@ -72,6 +74,7 @@ def forward(argv = None,
argv.remove(arg)
init_kwargs[key] = determine_type(value)
# any of the --flags are init kwargs

fn = argv.pop(0).replace('-', '_')
module = c.module(base)
fs = [fs for fs in fn_splitters if fs in fn]
Expand Down Expand Up @@ -132,5 +135,8 @@ def forward(argv = None,
else:
c.print(output)
return output


def main():
forward()
forward()

85 changes: 81 additions & 4 deletions commune/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ def request(self, url: str,
stream: bool = True):
try:
response = self.session.post(url, json=data, headers=headers, timeout=timeout, stream=stream)

if 'text/event-stream' in response.headers.get('Content-Type', ''):
return self.stream(response)
if 'application/json' in response.headers.get('Content-Type', ''):
Expand All @@ -132,7 +131,6 @@ def get_data(self, args=[], kwargs={}, **extra_kwargs):
kwargs = {**kwargs, **extra_kwargs}
data = { "args": args, "kwargs": kwargs}
data = self.serializer.serialize(data)

return data

def forward(self,
Expand All @@ -155,8 +153,12 @@ def forward(self,
'crypto_type': str(key.crypto_type),
'time': str(c.time())
}

headers['signature'] = key.sign({'data': headers['hash'], 'time': headers['time']}).hex()
return self.request(url=url, data=data,headers=headers, timeout=timeout)
return self.request(url=url,
data=data,
headers=headers,
timeout=timeout)

def __del__(self):
try:
Expand Down Expand Up @@ -223,4 +225,79 @@ def __getattr__(self, key):
return getattr(self, key)
else:
return lambda *args, **kwargs : self.remote_call(*args, remote_fn=key, **kwargs)



def forcurl(self,
fn: str = 'info',
args: list = None,
kwargs: dict = None,
timeout: int = 2,
key: str = None,
**extra_kwargs) -> str:
"""
Generate a cURL command for the equivalent HTTP request
Args:
fn (str): Function name to call
args (list): Arguments list
kwargs (dict): Keyword arguments
timeout (int): Request timeout in seconds
key (str): Key for authentication
**extra_kwargs: Additional keyword arguments
Returns:
str: cURL command string
"""
# Resolve the key and URL
key = self.resolve_key(key)
url = self.get_url(fn=fn)

# Prepare the data
data = self.get_data(args=args or [], kwargs=kwargs or {}, **extra_kwargs)

# Prepare headers
headers = {
'Content-Type': 'application/json',
'key': key.ss58_address,
'hash': c.hash(data),
'crypto_type': str(key.crypto_type),
'time': str(c.time())
}

# Add signature
headers['signature'] = key.sign({
'data': headers['hash'],
'time': headers['time']
}).hex()

# Build curl command
curl_cmd = ['curl']

# Add method
curl_cmd.append('-X POST')

# Add headers
for header_name, header_value in headers.items():
curl_cmd.append(f"-H '{header_name}: {header_value}'")

# Add data
if isinstance(data, str):
data_str = data
else:
data_str = json.dumps(data)
curl_cmd.append(f"-d '{data_str}'")

# Add URL
curl_cmd.append(f"'{url}'")

# Add timeout
curl_cmd.append(f'--max-time {timeout}')

# now get the dict of the response and return it
# make the request in the os and return the response
import os
response = os.popen(' '.join(curl_cmd)).read()


return response

Loading

0 comments on commit d61ea2c

Please sign in to comment.