public async Task <MultipleChoiceSet> CreateMultipleChoiceSet(MultipleChoiceSet multipleChoiceSet) { MultipleChoiceSet multipleChoiceSetDeserialize = null; HttpResponseMessage responseMessage; string multipleChoiceSetSerialized = JsonSerializer.Serialize(multipleChoiceSet); var content = new StringContent(multipleChoiceSetSerialized, Encoding.UTF8, "application/json"); // 1. Send POST request try { responseMessage = await client.PostAsync(uri + "/questionsets/createMultipleChoiceSet", content); // 2. Check if the resource was found, else throw exception to the client if (responseMessage.StatusCode == HttpStatusCode.NotFound) { throw new Exception("Ooops, resource not found"); } } // 3. Catch the exception in case the Server is not running catch (HttpRequestException e) { throw new Exception("No connection... Gfckyourself"); } string serverMessage = responseMessage.Content.ReadAsStringAsync().Result; // 4. Check the response status codes, else throws the error message to the client if (responseMessage.IsSuccessStatusCode) { // 5. Deserialize the object string readAsStringAsync = await responseMessage.Content.ReadAsStringAsync(); multipleChoiceSetDeserialize = JsonSerializer.Deserialize <MultipleChoiceSet>(readAsStringAsync); Console.WriteLine(multipleChoiceSetDeserialize.User.Username); } else if (responseMessage.StatusCode == HttpStatusCode.ServiceUnavailable) { throw new Exception(serverMessage); } else if (responseMessage.StatusCode == HttpStatusCode.Unauthorized) { throw new Exception(serverMessage); } return(multipleChoiceSetDeserialize); }
public async Task <MultipleChoiceSet> GetMultipleChoiceSetWithAllChildElements(long multipleChoiceSetId) { MultipleChoiceSet multipleChoiceSetDeserealize = null; HttpResponseMessage responseMessage; // 1. Send GET request try { responseMessage = await client.GetAsync($"{uri}/questionsets/getMultipleChoiceSetWithAllChildElements/{multipleChoiceSetId}"); // 2. Check if the resource was found, else throw exception to the client if (responseMessage.StatusCode == HttpStatusCode.NotFound) { throw new Exception("Ooops, resource not found"); } } // 3. Catch the exception in case the Server is not running catch (HttpRequestException e) { throw new Exception("No connection..."); } string serverMessage = responseMessage.Content.ReadAsStringAsync().Result; // 4. Check the response status codes, else throws the error message to the client if (responseMessage.IsSuccessStatusCode) { // 5. Deserialize the object string readAsStringAsync = await responseMessage.Content.ReadAsStringAsync(); multipleChoiceSetDeserealize = JsonSerializer.Deserialize <MultipleChoiceSet>(readAsStringAsync); } else if (responseMessage.StatusCode == HttpStatusCode.ServiceUnavailable) { throw new Exception(serverMessage); } return(multipleChoiceSetDeserealize); }