title: consuming REST service, method returning multiple types .. what to do
body: <p>I am consuming someone elses REST service for my app. the problem is that each request can return 1 of 3 different types when responding
  either: </p>
  
  <ol>
  <li>the expected successfull response type</li>
  <li>an error response which wraps the 500 (<code>Error</code>)</li>
  <li>a validation error response (<code>ValidationErrors</code>)</li>
  </ol>
  
  <p>I am currently calling the service wrapping each request with a class like this: </p>
  
  <pre><code>public class ApiResponse&lt;T&gt;
  {
      public T ResponseObject { get; set; }
      public ValidationErrors&lt;ValidationError&gt; Errors { get; set; }
      public Error Error { get; set; }
  }
  
  
  
   public async Task&lt;ApiResponse&lt;AMethodResponse&gt;&gt; AMethod(AMethodRequest req)
      {
          ApiResponse&lt;AMethodResponse&gt; resp = new ApiResponse&lt;AMethodResponse&gt; { Errors = new ValidationErrors&lt;ValidationError&gt;() };
          using (HttpClient client = HttpClientFactory.Create(new AuthorisationHandler(), new ContentTypeHandler()))
          {
              client.BaseAddress = new Uri(BaseURI);
  
              var httpResponseMessage = await client.PostAsXmlAsync&lt;AMethodRequest&gt;("AMethod/", req);
              if (!httpResponseMessage.IsSuccessStatusCode)
              {
  //its at this point that I need to work out if i am getting Validation Errors or.. a plain Error
  //I can do this, but of course if its a plain error it will fall over
                  resp.Errors = await httpResponseMessage.Content.ReadAsAsync&lt;ValidationErrors&lt;ValidationError&gt;&gt;();
              }
              else
              {
                  resp.ResponseObject = await httpResponseMessage.Content.ReadAsAsync&lt;AMethodResponse&gt;();
              }
          }
          return resp;
      }
  </code></pre>
  
  <p>I wonder if there is a more reliable pattern to writing consuming methods.</p>
  
  <p>thanks</p>