All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.robothy.sdwebui.sdk.utils.SdWebuiResponseUtils Maven / Gradle / Ivy

The newest version!
package io.github.robothy.sdwebui.sdk.utils;

import io.github.robothy.sdwebui.sdk.exceptions.SdWebuiBadRequestException;
import io.github.robothy.sdwebui.sdk.exceptions.SdWebuiServerValidationException;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpStatus;

public class SdWebuiResponseUtils {

  public static void checkResponseStatus(ClassicHttpResponse response) {
    if (response.getCode() == HttpStatus.SC_OK) {
      return;
    }

    try {
      if (response.getCode() == HttpStatus.SC_UNPROCESSABLE_ENTITY) {
        JsonUtils.fromJson(response.getEntity().getContent(), SdWebuiServerValidationException.class);
      }

      throw JsonUtils.fromJson(response.getEntity().getContent(), SdWebuiBadRequestException.class);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

  }

  public static  T parseResponse(ClassicHttpResponse response, Class clazz) {
    checkResponseStatus(response);
    try {
      return JsonUtils.fromJson(response.getEntity().getContent(), clazz);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy