com.lajospolya.spotifyapiwrapper.internal.Java11HttpResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spotify-api-wrapper Show documentation
Show all versions of spotify-api-wrapper Show documentation
This project wraps the Spotify public API in order to allow users to intuitively use it
package com.lajospolya.spotifyapiwrapper.internal;
import com.lajospolya.spotifyapiwrapper.response.SpotifyErrorContainer;
import com.lajospolya.spotifyapiwrapper.spotifyexception.SpotifyResponseException;
import java.lang.reflect.Type;
import java.net.http.HttpResponse;
public class Java11HttpResponse implements ISpotifyResponse
{
private final HttpResponseHelper helper;
private final HttpResponse response;
private final Type type;
private T body;
private SpotifyErrorContainer error;
private boolean erroneous = false;
public Java11HttpResponse(HttpResponse response, Type typeOfResponse)
{
this.helper = new HttpResponseHelper();
this.response = response;
this.type = typeOfResponse;
validateResponse();
}
private void validateResponse()
{
int statusCode = response.statusCode();
if(helper.isClientErrorStatusCode(statusCode) || helper.isServerErrorStatusCode(statusCode))
{
erroneous = true;
error = helper.serializeBody(response, SpotifyErrorContainer.class);
}
else
{
body = helper.serializeBody(response, type);
}
}
@Override
public T body() throws SpotifyResponseException
{
if(erroneous)
{
throw new SpotifyResponseException("Response was not successful");
}
return body;
}
@Override
public SpotifyErrorContainer error()
{
return error;
}
}