diff --git a/commune/client.py b/commune/client.py index b691fa20..ba6fa760 100644 --- a/commune/client.py +++ b/commune/client.py @@ -9,30 +9,21 @@ class Client(c.Module): - def __init__( self, module : str = 'module', - key : Optional[str]= None , + def __init__( self, + module : str = 'module', + key : Optional[str]= None, network: Optional[bool] = 'local', - mode: Optional[str] = 'http', serializer: Optional[c.Module] = 'serializer', - **kwargs + **kwargs ): self.serializer = c.module(serializer)() self.key = c.get_key(key, create_if_not_exists=True) - self.set_address(module, network=network, mode=mode) - - def set_address(self, module, network='local', mode='http'): if c.is_address(module): address = module else: - namespace = c.namespace(network=network) - if module in namespace: - address = namespace[module] - else: - raise Exception(f'Module {module} not found in namespace {namespace}') - prefix = f'{mode}://' + address = c.namespace(network=network).get(module) self.network = network - self.mode = mode - self.address = prefix + address if not address.startswith(prefix) else address + self.address = address self.session = requests.Session() @classmethod @@ -76,20 +67,14 @@ def test(self, module='module::test_client'): key = c.get_key(module) assert info['key'] == key.ss58_address return {'info': info, 'key': str(key)} - def get_url(self, fn, mode='http'): if '/' in str(fn): - module, fn = module.split('/') + address, fn = address.split('/') else: - module = self.module - module_address = self.address - ip = c.ip() - if ip in module_address: - module_address = module_address.replace(ip, '0.0.0.0') - url = f"{module_address}/{fn}/" - return url - + address = self.address + address = address if address.startswith(mode) else f'{mode}://{address}' + return f"{address}/{fn}/" def get_data(self, args=[], kwargs={}, params = None): # derefernece @@ -117,27 +102,16 @@ def forward(self, timeout:int=2, key : str = None, mode: str = 'http', - data=None, headers = None, stream:bool = False): key = self.resolve_key(key) url = self.get_url(fn=fn, mode=mode) - data = data or self.get_data(params=params, args=args, kwargs=kwargs, ) + data = self.get_data(params=params, args=args, kwargs=kwargs ) headers = headers or self.get_header(data=data, key=key) - try: + 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', ''): - result = response.json() - elif 'text/plain' in response.headers.get('Content-Type', ''): - result = response.text - else: - result = response.content - if response.status_code != 200: - raise Exception(result) - result = self.serializer.deserialize(result) + result = self.process_response(response) except Exception as e: result = c.detailed_error(e) return result @@ -155,17 +129,29 @@ def resolve_key(self,key=None): if isinstance(key, str): key = c.get_key(key) return key + + def process_response(self, response): + if 'text/event-stream' in response.headers.get('Content-Type', ''): + return self.stream(response) + if 'application/json' in response.headers.get('Content-Type', ''): + result = response.json() + elif 'text/plain' in response.headers.get('Content-Type', ''): + result = response.text + else: + result = response.content + if response.status_code != 200: + raise Exception(result) + result = self.serializer.deserialize(result) + return result def stream(self, response): try: for chunk in response.iter_lines(): - line = self.process_stream_line(chunk) - yield line + yield self.process_stream_line(chunk) except Exception as e: - print(f'Error in stream: {e}') - yield None + yield c.detailed_error(e) - def process_stream_line(self, line, stream_prefix = 'data: '): + def process_stream_line(self, line , stream_prefix = 'data: '): event_data = line.decode('utf-8') if event_data.startswith(stream_prefix): event_data = event_data[len(stream_prefix):] @@ -175,13 +161,6 @@ def process_stream_line(self, line, stream_prefix = 'data: '): if event_data.startswith('{') and event_data.endswith('}') and 'data' in event_data: event_data = json.loads(event_data)['data'] return event_data - - @staticmethod - def check_response(x) -> bool: - if isinstance(x, dict) and 'error' in x: - return False - else: - return True class Virtual: def __init__(self, client: str ='ReactAgentModule'): @@ -206,43 +185,4 @@ def get_header(self, data, key): } headers['signature'] = key.sign({'data': data, 'time': headers['time']}).hex() - return headers - - def forcurl(self, - fn: str = 'info', - args: list = None, - kwargs: dict = None, - timeout: int = 2, - key: str = None, - **extra_kwargs) -> str: - # 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) - headers = self.get_header(data=data, key=key) - # Prepare headers - - # Build curl command - curl_cmd = ['curl', '-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}'") - curl_cmd.append(f"'{url}'") - curl_cmd.append(f'--max-time {timeout}') - response = os.popen(' '.join(curl_cmd)).read() - return response - - def __str__ ( self ): - return "Client(address={})".format(self.address) - def __repr__ ( self ): - return self.__str__() \ No newline at end of file + return headers \ No newline at end of file diff --git a/scripts/build.sh b/scripts/build.sh index 106cd36f..f2d6f13b 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,4 +1,2 @@ - -PWD=$(pwd) -NAME=$(basename $PWD) -docker build -t $NAME $PWD \ No newline at end of file +NAME=$(basename $(pwd)) +docker build -t $NAME $(pwd) \ No newline at end of file diff --git a/scripts/enter.sh b/scripts/enter.sh index 2620964c..c287c401 100755 --- a/scripts/enter.sh +++ b/scripts/enter.sh @@ -1,3 +1,3 @@ -PWD=$(pwd) -NAME=$(basename $PWD) + +NAME=$(basename $(pwd)) docker exec -it $NAME /bin/bash \ No newline at end of file diff --git a/scripts/install.sh b/scripts/install.sh deleted file mode 100644 index b4f78c86..00000000 --- a/scripts/install.sh +++ /dev/null @@ -1,5 +0,0 @@ - - -# ensure npm is installed - -pip install -e ./ diff --git a/scripts/start.sh b/scripts/start.sh index 95c01e2a..ee010be4 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -15,4 +15,5 @@ fi --restart unless-stopped \ --privileged \ $NAME" + echo "Starting $NAME" eval docker run $CONTAINER_PARAMS \ No newline at end of file diff --git a/scripts/stop.sh b/scripts/stop.sh index 6ad70ebe..6295925c 100755 --- a/scripts/stop.sh +++ b/scripts/stop.sh @@ -1,12 +1,7 @@ - -PWD=$(pwd) -NAME=$(basename $PWD) -CONTAINER_NAME=$NAME -# RESOLVE PORT RANGE - +NAME=$(basename $(pwd)) CONTAINER_EXISTS=$(docker ps -q -f name=$NAME) if [ $CONTAINER_EXISTS ]; then - echo "STOPPING CONTAINER $NAME" + echo "STOPPING --> $NAME" docker kill $NAME CONTAINER_ID=$(docker ps -aq -f name=$NAME) docker rm $NAME diff --git a/scripts/test.sh b/scripts/test.sh index a7d25faa..d862de43 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -1,7 +1,7 @@ -PWD=$(pwd); NAME=$(basename $PWD) +NAME=$(basename $(pwd)); CONTAINER_EXISTS=$(docker ps -a | grep $NAME) if [ -z "$CONTAINER_EXISTS" ]; then - make run + ./scripts/start.sh fi docker exec -it $NAME pytest /$NAME/tests