Sunday 8 October 2017

Example of HttpClient

public List<ErrorDetail> CreateDocumentUDF(int registrationDocumentId, DocumentUDF documentUDF)
        {
            //Form the appropriate request uri for this call
            var requestUri = String.Format("{1}CreateDocumentUDF?registrationDocumentId={0}", registrationDocumentId, serviceUri);

            //Make the get call to web service layer
            HttpResponseMessage serviceResponse =
                Task<HttpResponseMessage>.Run(async () => { return await mdsClient.PostAsJsonAsync(requestUri, documentUDF); }).Result;
            //Asses the status code and return
            switch (serviceResponse.StatusCode)
            {
                case HttpStatusCode.Created:
                    //The call was successful, deserialize the response to a Property Document list
                    documentUDF = serviceResponse.Content.ReadAsAsync<DocumentUDF>().Result;
                    //No errors so return null
                    return null;
                default:
                    //if call have Error, deserialize the error list and return
                    documentUDF = null;
                    return serviceResponse.Content.ReadAsAsync<List<ErrorDetail>>().Result;
            }

        }

  public List<ErrorDetail> UpdateDocumentUDF(int registrationDocumentId, DocumentUDF documentUDF)
        {
            //Form the appropriate request uri for this call
            var requestUri = String.Format("{1}UpdateDocumentUDF?registrationDocumentId={0}", registrationDocumentId, serviceUri);
            //Asses the status code and return
            HttpResponseMessage serviceResponse =
                Task<HttpResponseMessage>.Run(async () => { return await mdsClient.PutAsJsonAsync(requestUri, documentUDF); }).Result;
            switch (serviceResponse.StatusCode)
            {
                case HttpStatusCode.NoContent:
                    //The call was successful, deserialize the response to a RegistrationDistrict list
                    documentUDF = serviceResponse.Content.ReadAsAsync<DocumentUDF>().Result;
                    //No errors so return null
                    return null;

                default:
                    //if call have Error, deserialize the error list and return
                    documentUDF = null;
                    return serviceResponse.Content.ReadAsAsync<List<ErrorDetail>>().Result;
            }

        }

public List<ErrorDetail> DeleteDocumentUDF(int registrationDocumentId, int documentUDFMappingId)
        {
            //Form the appropriate request uri for this call
            var requestUri = String.Format("{2}DeleteDocumentUDF?registrationDocumentId={0}&documentUdfMappingId={1}", registrationDocumentId, documentUDFMappingId, serviceUri);
            //Make the get call to web service layer
            HttpResponseMessage serviceResponse =
               Task<HttpResponseMessage>.Run(async () => { return await mdsClient.DeleteAsync(requestUri); }).Result;
            //Asses the status code and return
            switch (serviceResponse.StatusCode)
            {
                //The call was successful, deserialize the response to a Zone list
                //No errors so return null
                case HttpStatusCode.NoContent:
                    return null;

                default:
                    //if call have Error, deserialize the error list and return
                    return serviceResponse.Content.ReadAsAsync<List<ErrorDetail>>().Result;
            }
        }



public List<ErrorDetail> GetDocumentUDF(int registrationDocumentId, int languageId, out List<DocumentUDF> documentUDF)
        {
            //Form the appropriate request uri for this call
            var requestUri = String.Format("{2}GetDocumentUDF?registrationDocumentId={0}&languageId={1}", registrationDocumentId, languageId, serviceUri);
            //Make the get call to web service layer
            HttpResponseMessage serviceResponse =
                Task<HttpResponseMessage>.Run(async () => { return await mdsClient.GetAsync(requestUri); }).Result;

            //Asses the status code and return
            switch (serviceResponse.StatusCode)
            {
                case HttpStatusCode.OK:
                    //The call was successful, deserialize the response to a Property Document list
                    documentUDF = serviceResponse.Content.ReadAsAsync<List<DocumentUDF>>().Result;
                    //No errors so return null
                    return null;

                default:
                    //if call have Error, deserialize the error list and return
                    documentUDF = null;
                    return serviceResponse.Content.ReadAsAsync<List<ErrorDetail>>().Result;
            }

        }



public List<ErrorDetail> GetDocumentUdfById(int registrationDocumentId, int udfId, int languageId, out DocumentUDF documentUdf)
        {
            //Form the appropriate request uri for this call
            var requestUri = String.Format("{3}GetDocumentUDFById?registrationDocumentId={0}&languageId={1}&udfId={2}", registrationDocumentId, languageId, udfId, serviceUri);
            //Make the get call to web service layer
            HttpResponseMessage serviceResponse =
                Task<HttpResponseMessage>.Run(async () => { return await mdsClient.GetAsync(requestUri); }).Result;

            //Asses the status code and return
            switch (serviceResponse.StatusCode)
            {
                case HttpStatusCode.OK:
                    //The call was successful, deserialize the response to a Property Document list
                    documentUdf = serviceResponse.Content.ReadAsAsync<DocumentUDF>().Result;
                    //No errors so return null
                    return null;

                default:
                    //if call have Error, deserialize the error list and return
                    documentUdf = null;
                    return serviceResponse.Content.ReadAsAsync<List<ErrorDetail>>().Result;
            }
        }


No comments:

Post a Comment