API Reference

Client

Attributes
class winerp.client.Client(local_name, host='localhost', port=13254, reconnect=True)

Represents a winerp Client. This class is used to interact with the Server

Parameters
  • local_name (str) – The name which will be used to refer to this client. This should be unique to all the clients.

  • host (Optional[str]) – The port on which the server is running. Defaults to localhost.

  • port (Optional[int]) – The port on which the server is running. Defaults to 13254.

  • reconnect (Optional[bool]) – If set to True, the client will automatically try to reconnect to the winerp server every 60 seconds (default). This option is set to True by default.

@route

A decorator to register your route. The route name should be unique.

Raises
  • ValueError – Route name already exists.

  • InvalidRouteType – The function passed is not a coro.

@client.Client().event()

Represents a winerp Client. This class is used to interact with the Server

Parameters
  • local_name (str) – The name which will be used to refer to this client. This should be unique to all the clients.

  • host (Optional[str]) – The port on which the server is running. Defaults to localhost.

  • port (Optional[int]) – The port on which the server is running. Defaults to 13254.

  • reconnect (Optional[bool]) – If set to True, the client will automatically try to reconnect to the winerp server every 60 seconds (default). This option is set to True by default.

property authorized

Returns if the client is authorized by the server.

Type

bool

property on_hold

Returns True if the client is on hold by the server. A client is put on hold if a client of same local name is already connected to the server.

Type

bool

await send_message(data)

Send a message to the server.

await start()

This function is a coroutine.

Connects the client to the server.

Raises

ConnectionError – If the websocket is already connected.

Returns

Return type

None

await add_route(callback, name=None)

This function is a coroutine. A function to register a route. Either a decorator or this function can be used to register a route.

Parameters
  • callback

  • name

Returns

Return type

typing.Callable

Raises
  • KeyError – Route name already exists.

  • InvalidRouteType – The function passed is not a coro.

remove_route(name)

Removes a route from the registered routes.

Parameters

name – The name of the route to be removed.

Returns

Return type

None

Raises

KeyError – Route name does not exist.

await ping(client=None, timeout=60)

This function is a coroutine.

Pings the client and returns back if the ping was successful.

Raises
Returns

If the ping is successful, it returns True.

Return type

bool

await request(route, source, timeout=60, **kwargs)

This function is a coroutine.

Requests the server for a response. Resolves when the response is received matching the UUID.

Parameters
  • route (str) – The route to request to.

  • source (str) – The destination

  • timeout (int) – Time to wait before raising TimeoutError.

Raises
  • ClientNotReadyError – The client is currently not ready to send or accept requests.

  • UnauthorizedError – The client isn’t authorized by the server.

  • ValueError: – Missing either route or source or both.

  • RuntimeError – If the UUID is not found.

  • asyncio.TimeoutError – If the response is not received within the timeout.

Returns

The data associated with the message.

Return type

Any

await inform(data, destinations)

This function is a coroutine.

Sends data to other connected clients. There is no tracking of the data so there won’t be any error if it doesn’t reach its specified destination.

The data is sent to all connected clients if the destinations list is empty.

Parameters
  • data (Any) – The data to redirect.

  • destinations (list) – The list of destinations.

Raises
Returns

Return type

None

await wait_until_ready()

This function is a coroutine.

Waits until the client is ready to send or accept requests.

await wait_until_disconnected()

This function is a coroutine.

Waits until the client is disconnected.

wait_for(event, timeout=None)

This function is a coroutine.

Waits for a WebSocket event to be dispatched.

The timeout parameter is passed onto asyncio.wait_for(). By default, it does not have timeout.

In case the event returns multiple arguments, a tuple containing those arguments is returned instead. Please check the documentation for a list of events and their parameters.

This function returns the first event that meets the requirements.

Parameters
  • event (str) – The event to wait for.

  • timeout (Optional[int]) – Time to wait before raising TimeoutError. Defaults to 60.

Raises

asyncio.TimeoutError – If the event is not received within the timeout.

Returns

The payload for the event that meets the requirements.

Return type

Any

Server

Attributes
Methods
class winerp.server.Server(host='127.0.0.1', port=13254)

Represents a winerp Server. This class is used as the central communication center for all connected clients. All requests and responses pass through the server

If the library is installed using PyPi, you can also use terminal to start server using winerp –port 1234

Parameters
  • host (Optional[str]) – The host on which the server is running. Defaults to 127.0.0.1.

  • port (Optional[int]) – The port on which the server is running. Defaults to 13254.

property client_count

Returns the number of connected clients

Type

int

start()

Starts the server on the given port.

Errors

exception winerp.lib.errors.UnauthorizedError

Raised when the client is trying to make a request without being authorized by the server.

exception winerp.lib.errors.UUIDNotFoundError

Raised when the specific UUID is not found in listeners.

exception winerp.lib.errors.MissingUUIDError

Raised when the message does not have a uuid key.

exception winerp.lib.errors.CheckFailureError

Raised when the check fails while the message is being dispatched.

exception winerp.lib.errors.InvalidRouteType

Raised when the route type is not valid.

exception winerp.lib.errors.ClientRuntimeError

Raised when the server returns back an error.

exception winerp.lib.errors.ClientNotReadyError

Raised when the client is not ready to send requests