001/*
002 * Copyright (c) 2010-2026 Mark Allen, Norbert Bartels.
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.restfb;
023
024import java.io.Serializable;
025import java.time.Duration;
026import java.util.List;
027import java.util.Map;
028import java.util.Optional;
029
030/**
031 * Carries the result of a Graph API call plus response metadata such as debug headers.
032 *
033 * @param <T>
034 *          type of the mapped result
035 */
036public final class ApiResult<T> implements Serializable {
037
038  private static final long serialVersionUID = 1L;
039
040  private final T result;
041
042  private final ResponseMetadata responseMetadata;
043
044  private ApiResult(T result, ResponseMetadata responseMetadata) {
045    this.result = result;
046    this.responseMetadata = responseMetadata;
047  }
048
049  public static <T> ApiResult<T> withMetadata(T result, DebugHeaderInfo debugHeaderInfo,
050      Map<String, List<String>> responseHeaders, Duration duration, String httpMethod, String requestUrl) {
051    return withMetadata(result,
052      ResponseMetadata.of(debugHeaderInfo, responseHeaders, duration, httpMethod, requestUrl));
053  }
054
055  public static <T> ApiResult<T> withMetadata(T result, ResponseMetadata responseMetadata) {
056    return new ApiResult<>(result, responseMetadata);
057  }
058
059  public static <T> ApiResult<T> withoutMetadata(T result) {
060    return new ApiResult<>(result, null);
061  }
062
063  public T getResult() {
064    return result;
065  }
066
067  public ResponseMetadata getResponseMetadata() {
068    return responseMetadata;
069  }
070
071  public DebugHeaderInfo getDebugHeaderInfo() {
072    return Optional.ofNullable(responseMetadata).map(ResponseMetadata::getDebugHeaderInfo).orElse(null);
073  }
074
075  public Map<String, List<String>> getResponseHeaders() {
076    return Optional.ofNullable(responseMetadata).map(ResponseMetadata::getResponseHeaders).orElse(null);
077  }
078
079  public Duration getDuration() {
080    return Optional.ofNullable(responseMetadata).map(ResponseMetadata::getDuration).orElse(null);
081  }
082
083  public String getHttpMethod() {
084    return Optional.ofNullable(responseMetadata).map(ResponseMetadata::getHttpMethod).orElse(null);
085  }
086
087  public String getRequestUrl() {
088    return Optional.ofNullable(responseMetadata).map(ResponseMetadata::getRequestUrl).orElse(null);
089  }
090}