-
Notifications
You must be signed in to change notification settings - Fork 0
Home
The wiki is currently WIP. Stay tuned
RepetierSharp uses a
- HttpClient RestSharps RestClient as well as a
- WebSocketClient WebSocketClient from Websocket.Client
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.
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.
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.
RepetierConnection rc = new RepetierConnectionBuilder()
.WithWebsocketHost("wss://example.com:1234")
.WithHttpRestHost("https://example.com:5678")
.WithApiKey("your-api-key")
.Build();
await rc.Connect();
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();
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();
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();
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();
For more advanced configuration and setting up event handlers take a look at the respective pages.