Skip to content
Marcel Bartholet edited this page Jul 18, 2024 · 4 revisions

The wiki is currently WIP. Stay tuned

Introduction

Establishing a connection

RepetierSharp uses a

for communicating with the Repetier Server.

Simply start by creating an instance of the RepetierConnection class. There are multiple ways to establish a valid connection. The following section describes them in detail with code examples.

Basic configuration without authentication

The most basic configuration to set up a working RepetierConnection looks like this:

var rc = new RepetierConnectionBuilder()
    .WithWebsocketHost("wss://example.com:1234")
    .WithHttpRestHost("https://example.com:5678")
    .Build();

rc.Connect();

This gives you access to the repetier server with the global user profile. This only works if there are no user defined at the server.

In most cases you would want to create a connection by supplying an API-Key or user credentials.

Using an API Key

Example using an API-Key for authentication. This can either be the Repetier-Server or the user specific API-Key. The method WithApiKey is used to provide the API-Key for both the HTTP and WebSocket connection.

Supplying the API-Key for both connections

RepetierConnection rc = new RepetierConnectionBuilder()
    .WithWebsocketHost("wss://example.com:1234")
    .WithHttpRestHost("https://example.com:5678")
    .WithApiKey("your-api-key")
    .Build();

await rc.Connect();

Supplying the API-Key separately for HTTP and WebSocket

var apiKey = "your-api-key";
var restClientOptions = new RestClientOptions()
{
    BaseUrl = new Uri("https://example.com"),
    Authenticator = new RepetierApiKeyRequestHeaderAuthenticator(apiKey)
};
var restClient = new RestClient(restClientOptions);

var rc = new RepetierConnectionBuilder()
    .WithWebsocketHost($"wss://example.com/socket/?apiKey={apiKey}")
    .UseRestClient(restClient)
    .Build();

await rc.Connect();

Basic configuration providing user credentials

In case there are user profiles defined, and you provide no API-Key when connecting, the server will respond with a loginRequired event. You can then use the Login command to authenticate with the server by supplying your credentials.

Alternatively, you can already provide the user credentials when creating the connection with the builder. This will automatically authenticate you with the server as soon as the loginRequired event is fired. The provided password will be hashed according to the Repetier Server API.

Note that, to upload gcode via the REST-API, you need to authenticate also supply an API-Key for the HTTP-Client. Preferably in the HTTP-Header.

Example using user credentials:

var rc = new RepetierConnectionBuilder()
	.WithWebsocketHost($"wss://example.com/socket/")
	.WithWebsocketAuth("user", "password", rememberSession: true)
	.Build();

await rc.Connect();

Example supplying RestClient and WebsocketClient:

 var apiKey = "your-api-key";
 var restClientOptions = new RestClientOptions()
 {
     BaseUrl = new Uri("https://example.com"),
     Authenticator = new RepetierApiKeyRequestHeaderAuthenticator(apiKey)
 };
 var restClient = new RestClient(restClientOptions);
 
 var websocketClient = new WebSocketClient($"wss://example.com/socket/");
 
 // .. do more complex setup with your clients
 
 var rc = new RepetierConnectionBuilder()
     .UseWebSocketClient(websocketClient)
     .WithWebsocketAuth("user", "password", rememberSession: true)
     .UseRestClient(restClient)
     .Build();
 
 await rc.Connect();

Using an existing session

In case you already have a session established, you can use this sessionId to establish a connection with the same session.

Note that at time you are trying to connect with an existing session, the session must be still open for another client.

This example shows how to connect with an existing session. There is no authentication for the websocket provided beside the sessionId. The HTTP-Client still needs to be authenticated with the API-Key if you want to use direct API calls to upload and start prints.

If the server responds with a loginRequired event the provided session is most likely invalid or expired.

 var sessionId = "your-session-id";
 var restClientOptions = new RestClientOptions()
 {
     BaseUrl = new Uri("https://example.com"),
     Authenticator = new RepetierApiKeyRequestHeaderAuthenticator("your-api-key")
 };
 var restClient = new RestClient(restClientOptions);
 
 var rc = new RepetierConnectionBuilder()
     .WithWebsocketHost($"wss://example.com/socket/")
     .UseRestClient(restClient)
     .WithSession(sessionId)
     .Build();
 
 await rc.Connect();

Further connection configuration

For more advanced configuration and setting up event handlers take a look at the respective pages.