Page 1 of 1

Jiwa-stateful

PostPosted: Fri Jun 30, 2017 1:05 pm
by SBarnes
Hi Mike

to make the API operate with state you use the following code:

Code: Select all
client.Headers.Add("jiwa-stateful", "true");


is there a way to make the API operate in a mixed mode i.e by doing something like

Code: Select all
client.Headers.Remove("jiwa-stateful");


or do you need to log off and log back on again, what I am asking is is the header checked each time and behaviour determined at the time of the call or is it held somehow to the session?

Re: Jiwa-stateful  Topic is solved

PostPosted: Fri Jun 30, 2017 1:09 pm
by Mike.Sheen
Hi Stuart,

You can remove them at any time, and the next call won't include them.

Here's an example from our unit tests which use the ServiceStack client to do exactly that:
Code: Select all
[Test]
public void Carriers_Stateful_Create_Save()
{
   client.Headers.Add("jiwa-stateful", "true");

   // Try before logging on to make sure we get a 401
   var carrierPOSTRequest = new CarrierPOSTRequest { CarrierName = Guid.NewGuid().ToString(), Enabled = false };
   Carrier carriersPOSTResponse;
   var ex = Assert.Throws<ServiceStack.WebServiceException>(() => carriersPOSTResponse = client.Post(carrierPOSTRequest));
   Assert.AreEqual(401, ex.StatusCode);

   Logon();

   // POST: /Carriers
   carrierPOSTRequest = new CarrierPOSTRequest { CarrierName = Guid.NewGuid().ToString(), Enabled = false };
   carriersPOSTResponse = client.Post(carrierPOSTRequest);

   // Check the response has the same name as the request
   Assert.AreEqual(carriersPOSTResponse.CarrierName, carrierPOSTRequest.CarrierName);

   // Now do a non-stateful read to make sure it's not present yet
   // GET: /Carriers/{CarrierID}
   client.Headers.Remove("jiwa-stateful");
   var carrierGETRequest = new CarrierGETRequest() { CarrierID = carriersPOSTResponse.CarrierID };
   // We expect a 404 when reading
   Carrier carrierGETResponse;
   ex = Assert.Throws<ServiceStack.WebServiceException>(() => carrierGETResponse = client.Get(carrierGETRequest));
   Assert.AreEqual(404, ex.StatusCode);

   // Add back the stateful header
   client.Headers.Add("jiwa-stateful", "true");

   // GET: /Carriers/{CarrierID}/Save
   var carrierSAVERequest = new CarrierSAVERequest() { CarrierID = carriersPOSTResponse.CarrierID };
   var carrierSAVEResponse = client.Get(carrierSAVERequest);

   // GET: /Carriers/{CarrierID}
   carrierGETRequest = new CarrierGETRequest() { CarrierID = carrierSAVEResponse.CarrierID };
   carrierGETResponse = client.Get(carrierGETRequest);

   // Check the Name is what we expect
   Assert.AreEqual(carrierGETResponse.CarrierName, carrierPOSTRequest.CarrierName);
}


EDIT: To answer your question, the ServiceStack client will include them for every request once set, until removed.

Re: Jiwa-stateful

PostPosted: Fri Jun 30, 2017 1:23 pm
by SBarnes
Cheers