You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.8 KiB
69 lines
1.8 KiB
package common.ecommerce.http; |
|
|
|
import java.util.Collections; |
|
import java.util.LinkedHashMap; |
|
import java.util.Map; |
|
|
|
/** |
|
* 统一响应对象。保留原始 header/body 供留痕与错误映射。 |
|
*/ |
|
public final class HttpResponse { |
|
|
|
private final int statusCode; |
|
private final Map<String, String> headers; |
|
private final String body; |
|
private final long latencyMillis; |
|
|
|
private HttpResponse(int statusCode, Map<String, String> headers, String body, long latencyMillis) { |
|
this.statusCode = statusCode; |
|
this.headers = Collections.unmodifiableMap(new LinkedHashMap<String, String>(headers)); |
|
this.body = body; |
|
this.latencyMillis = latencyMillis; |
|
} |
|
|
|
public static HttpResponse of(int statusCode, Map<String, String> headers, String body, long latencyMillis) { |
|
return new HttpResponse(statusCode, headers == null ? Collections.<String, String>emptyMap() : headers, |
|
body, latencyMillis); |
|
} |
|
|
|
public int statusCode() { |
|
return statusCode; |
|
} |
|
|
|
public Map<String, String> headers() { |
|
return headers; |
|
} |
|
|
|
public String body() { |
|
return body; |
|
} |
|
|
|
public long latencyMillis() { |
|
return latencyMillis; |
|
} |
|
|
|
public boolean isSuccess() { |
|
return statusCode >= 200 && statusCode < 300; |
|
} |
|
|
|
public boolean isAuthFailure() { |
|
return statusCode == 401 || statusCode == 403; |
|
} |
|
|
|
public boolean isRateLimited() { |
|
return statusCode == 429; |
|
} |
|
|
|
public boolean isServerError() { |
|
return statusCode >= 500; |
|
} |
|
|
|
public String header(String name) { |
|
for (Map.Entry<String, String> e : headers.entrySet()) { |
|
if (e.getKey().equalsIgnoreCase(name)) { |
|
return e.getValue(); |
|
} |
|
} |
|
return null; |
|
} |
|
}
|
|
|