Once you have your Juzu server up and running, let’s see how we can use the SDK to connect to it.
First, you need to know the address (host
:port
) where the server is running.
This document will assume the values 127.0.0.1:2727
, but be sure to change
those to point to your server instance.
The following code snippet connects to the server and queries its version. It uses our recommended default setup, expecting the server to be listening on a TLS encrypted connection. Examples showing how to connect to a server not using TLS is also shown in the Insecure Connection section.
<div class="highlight"><pre style="color:#d0d0d0;background-color:#202020;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-py" data-lang="py"><span style="color:#6ab825;font-weight:bold">import</span> <span style="color:#447fcf;text-decoration:underline">juzu</span>
serverAddress = <span style="color:#ed9d13">"127.0.0.1:2727"</span>
client = juzu.Client(serverAddress)
resp = client.Version()
<span style="color:#6ab825;font-weight:bold">print</span>(resp)
</code></pre></div>
<div class="highlight"><pre style="color:#d0d0d0;background-color:#202020;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-csharp" data-lang="csharp">
<span style="color:#6ab825;font-weight:bold">var</span> serverAddress = <span style="color:#ed9d13">"127.0.0.1:2727"</span>;
<span style="color:#999;font-style:italic">// change this to true if using server without TLS
</span><span style="color:#999;font-style:italic"></span><span style="color:#6ab825;font-weight:bold">var</span> insecure = <span style="color:#6ab825;font-weight:bold">false</span>;
<span style="color:#6ab825;font-weight:bold">var</span> client = <span style="color:#6ab825;font-weight:bold">new</span> Client (serverAddress, insecure);
<span style="color:#999;font-style:italic">// Get Version of the server
</span><span style="color:#999;font-style:italic"></span><span style="color:#6ab825;font-weight:bold">var</span> ver = client.Version ();
Console.WriteLine (<span style="color:#ed9d13">"Juzu: {0} Server: {1}"</span>, ver.Juzu, ver.Server);
</code></pre></div>
It is sometimes required to connect to Juzu server without TLS enabled, such as during debugging.
Please note that if the server has TLS enabled, attempting to connect with an insecure client will fail. To connect to an instance of Juzu server without TLS enabled, you can use:
<div class="highlight"><pre style="color:#d0d0d0;background-color:#202020;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-py" data-lang="py">client = juzu.Client(serverAddress, insecure=True)
</code></pre></div>
<div class="highlight"><pre style="color:#d0d0d0;background-color:#202020;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-csharp" data-lang="csharp"><span style="color:#6ab825;font-weight:bold">var</span> insecure = <span style="color:#6ab825;font-weight:bold">true</span>;
<span style="color:#6ab825;font-weight:bold">var</span> client = <span style="color:#6ab825;font-weight:bold">new</span> Client (serverAddress, insecure);
</code></pre></div>
In our recommended default setup, TLS is enabled in the gRPC setup, and when connecting to the server, clients validate the server’s SSL certificate to make sure they are talking to the right party. This is similar to how “https” connections work in web browsers.
In some setups, it may be desired that the server should also validate clients connecting to it and only respond to the ones it can verify. If your Juzu server is configured to do client authentication, you will need to present the appropriate certificate and key when connecting to it.
Please note that in the client-authentication mode, the client will still also verify the server’s certificate, and therefore this setup uses mutually authenticated TLS. This can be done with:
<div class="highlight"><pre style="color:#d0d0d0;background-color:#202020;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-py" data-lang="py">client = juzu.Client(serverAddress, clientCertificate=certPem, clientKey=keyPem)
</code></pre></div>
<h4 id="authenticating-server-certificate">Authenticating Server Certificate</h4>
<div class="highlight"><pre style="color:#d0d0d0;background-color:#202020;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-csharp" data-lang="csharp"><span style="color:#6ab825;font-weight:bold">var</span> rootPem = File.ReadAllText(<span style="color:#ed9d13">"root.pem"</span>);
<span style="color:#6ab825;font-weight:bold">var</span> client = <span style="color:#6ab825;font-weight:bold">new</span> Client (serverAddress, rootPem);
</code></pre></div><h4 id="mutual-authentication">Mutual Authentication</h4>
<div class="highlight"><pre style="color:#d0d0d0;background-color:#202020;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-csharp" data-lang="csharp"><span style="color:#6ab825;font-weight:bold">var</span> rootPem = File.ReadAllText(<span style="color:#ed9d13">"root.pem"</span>);
<span style="color:#6ab825;font-weight:bold">var</span> certPem = File.ReadAllText(<span style="color:#ed9d13">"cert.pem"</span>);
<span style="color:#6ab825;font-weight:bold">var</span> keyPem = File.ReadAllText(<span style="color:#ed9d13">"key.pem"</span>);
<span style="color:#6ab825;font-weight:bold">var</span> client = <span style="color:#6ab825;font-weight:bold">new</span> Client (serverAddress, rootPem, certPem, keyPem);
</code></pre></div>
where rootPem is the bytes of the certificate used to validate the server certificate and certPem & keyPem are the bytes of the client certificate and key provided to you respectively.