001// Generated by delombok at Wed Jan 31 21:27:42 UTC 2024
002/*
003 * Copyright (c) 2010-2024 Mark Allen, Norbert Bartels.
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a copy
006 * of this software and associated documentation files (the "Software"), to deal
007 * in the Software without restriction, including without limitation the rights
008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009 * copies of the Software, and to permit persons to whom the Software is
010 * furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021 * THE SOFTWARE.
022 */
023package com.restfb;
024
025import java.io.Serializable;
026import java.util.ArrayList;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030import com.restfb.json.Json;
031import com.restfb.json.JsonObject;
032import com.restfb.util.StringUtils;
033
034public class DebugHeaderInfo implements Serializable {
035  /**
036   * x-fb-debug
037   */
038  private final String debug;
039  /**
040   * x-fb-rev
041   */
042  private final String rev;
043  /**
044   * x-fb-trace-id
045   */
046  private final String traceId;
047  /**
048   * the use version
049   */
050  private final Version usedVersion;
051  /**
052   * x-app-usage
053   */
054  private final HeaderUsage appUsage;
055  /**
056   * x-page-usage
057   */
058  private final HeaderUsage pageUsage;
059  /**
060   * x-ad-account-usage
061   */
062  private final HeaderUsage adAccountUsage;
063  private final BusinessUseCaseUsage businessUseCaseUsage;
064
065  private DebugHeaderInfo(String debug, String rev, String traceId, Version version, String appUsage, String pageUsage, String adAccountUsage, String businessUsage) {
066    this.debug = debug;
067    this.rev = rev;
068    this.traceId = traceId;
069    this.usedVersion = version;
070    this.appUsage = createUsage(appUsage);
071    this.pageUsage = createUsage(pageUsage);
072    this.adAccountUsage = createUsage(adAccountUsage);
073    this.businessUseCaseUsage = createBusinessUsage(businessUsage);
074  }
075
076  /**
077   * get Facebook response header field <code>x-fb-debug</code>
078   * 
079   * @return the Facebook response header field <code>x-fb-debug</code>
080   */
081  public String getDebug() {
082    return debug;
083  }
084
085  /**
086   * get Facebook response header field <code>x-fb-rev</code>
087   * 
088   * @return the Facebook response header field <code>x-fb-rev</code>
089   */
090  public String getRev() {
091    return rev;
092  }
093
094  /**
095   * get Facebook response header field <code>x-fb-trace-id</code>
096   * 
097   * @return the Facebook response header field x-fb-trace-id
098   */
099  public String getTraceId() {
100    return traceId;
101  }
102
103  /**
104   * get the version, facebook used internally to fulfill the request
105   * 
106   * @return the version, facebook used internally to fulfill the request
107   */
108  public Version getUsedVersion() {
109    return usedVersion;
110  }
111
112  /**
113   * get Facebook response header field <code>x-app-usage</code>
114   *
115   * @return the Facebook response header field x-app-usage
116   */
117  public HeaderUsage getAppUsage() {
118    return appUsage;
119  }
120
121  /**
122   * get Facebook response header field <code>x-page-usage</code>
123   *
124   * @return the Facebook response header field x-page-usage
125   */
126  public HeaderUsage getPageUsage() {
127    return pageUsage;
128  }
129
130  /**
131   * get Facebook response header field <code>x-ad-account-usage</code>
132   * 
133   * @return the Facebook response header field x-ad-account-usage
134   */
135  public HeaderUsage getAdAccountUsage() {
136    return adAccountUsage;
137  }
138
139  public BusinessUseCaseUsage getBusinessUseCaseUsage() {
140    return businessUseCaseUsage;
141  }
142
143  private BusinessUseCaseUsage createBusinessUsage(String businessUsageInformation) {
144    if (StringUtils.isBlank(businessUsageInformation)) {
145      return null;
146    }
147    try {
148      DefaultJsonMapper jsonMapper = new DefaultJsonMapper();
149      JsonObject jsonObject = Json.parse(businessUsageInformation).asObject();
150      BusinessUseCaseUsage usage = new BusinessUseCaseUsage();
151      for (String key : jsonObject.names()) {
152        usage.put(key, jsonMapper.toJavaList(jsonObject.get(key).toString(), InnerBusinessUseCaseUsage.class));
153      }
154      return usage;
155    } catch (Exception e) {
156      return null;
157    }
158  }
159
160  private HeaderUsage createUsage(String usageInformation) {
161    if (StringUtils.isBlank(usageInformation)) {
162      return null;
163    }
164    try {
165      JsonObject jsonValue = Json.parse(usageInformation).asObject();
166      if (jsonValue.names().size() == 1 && jsonValue.contains("acc_id_util_pct")) {
167        return new HeaderUsage(jsonValue.getDouble("acc_id_util_pct", 0.0));
168      }
169      return new HeaderUsage(jsonValue.getInt("call_count", -1), jsonValue.getInt("total_time", -1), jsonValue.getInt("total_cputime", -1));
170    } catch (Exception e) {
171      return new HeaderUsage(usageInformation);
172    }
173  }
174
175
176  /**
177   * business use case based usage object.
178   *
179   * This usage object is used in Graph API 3.3+ and provides access to the complete set of inner objects.
180   */
181  public static class BusinessUseCaseUsage implements Serializable {
182    private Map<String, List<InnerBusinessUseCaseUsage>> usageMap = new HashMap<>();
183
184    /**
185     * add a new list of usages that belongs to the business id to the outer object
186     *
187     * @param key
188     *          the business id the list of usages is connected with
189     * @param businessUseCaseUsageList
190     *          the list of usages
191     */
192    void put(String key, List<InnerBusinessUseCaseUsage> businessUseCaseUsageList) {
193      usageMap.put(key, businessUseCaseUsageList);
194    }
195
196    /**
197     * returns the usage list that belongs to the given business id
198     *
199     * @param businessId
200     *          the business ids you like to fetch the usage list for
201     * @return the list of usages
202     */
203    public List<InnerBusinessUseCaseUsage> get(String businessId) {
204      return usageMap.get(businessId);
205    }
206
207    /**
208     * returns the list of Business Ids that are provided in the the business use case usage header
209     * 
210     * @return list of business ids
211     */
212    public List<String> getBusinessIds() {
213      return new ArrayList<>(usageMap.keySet());
214    }
215  }
216
217
218  public static class InnerBusinessUseCaseUsage implements Serializable {
219    /**
220     * Percentage of calls made for this business id/business ad account
221     */
222    @Facebook("call_count")
223    private Integer callCount;
224    /**
225     * Percentage of the total CPU time that has been used
226     */
227    @Facebook("total_cputime")
228    private Integer totalCputime;
229    /**
230     * Percentage of the total time that has been used
231     */
232    @Facebook("total_time")
233    private Integer totalTime;
234    /**
235     * Type of rate limit logic being applied
236     */
237    @Facebook
238    private String type;
239    /**
240     * Time in minutes to resume calls
241     */
242    @Facebook("estimated_time_to_regain_access")
243    private Integer estimatedTimeToRegainAccess;
244
245    /**
246     * Percentage of calls made for this business id/business ad account
247     */
248    @java.lang.SuppressWarnings("all")
249    public Integer getCallCount() {
250      return this.callCount;
251    }
252
253    /**
254     * Percentage of the total CPU time that has been used
255     */
256    @java.lang.SuppressWarnings("all")
257    public Integer getTotalCputime() {
258      return this.totalCputime;
259    }
260
261    /**
262     * Percentage of the total time that has been used
263     */
264    @java.lang.SuppressWarnings("all")
265    public Integer getTotalTime() {
266      return this.totalTime;
267    }
268
269    /**
270     * Type of rate limit logic being applied
271     */
272    @java.lang.SuppressWarnings("all")
273    public String getType() {
274      return this.type;
275    }
276
277    /**
278     * Time in minutes to resume calls
279     */
280    @java.lang.SuppressWarnings("all")
281    public Integer getEstimatedTimeToRegainAccess() {
282      return this.estimatedTimeToRegainAccess;
283    }
284  }
285
286
287  public static class HeaderUsage implements Serializable {
288    HeaderUsage(String percentage) {
289      this.percentage = percentage;
290      this.percentageOnly = true;
291      this.adAccountHeader = false;
292    }
293
294    HeaderUsage(Integer callCount, Integer totalTime, Integer totalCputime) {
295      this.callCount = callCount;
296      this.totalTime = totalTime;
297      this.totalCputime = totalCputime;
298      this.percentageOnly = false;
299      this.adAccountHeader = false;
300    }
301
302    HeaderUsage(Double percentage) {
303      this.accIdUtilPct = percentage;
304      this.percentageOnly = false;
305      this.adAccountHeader = true;
306    }
307
308    private final boolean percentageOnly;
309    private final boolean adAccountHeader;
310    private String percentage;
311    private Integer callCount;
312    private Integer totalTime;
313    private Integer totalCputime;
314    private Double accIdUtilPct;
315
316    @java.lang.SuppressWarnings("all")
317    public boolean isPercentageOnly() {
318      return this.percentageOnly;
319    }
320
321    @java.lang.SuppressWarnings("all")
322    public boolean isAdAccountHeader() {
323      return this.adAccountHeader;
324    }
325
326    @java.lang.SuppressWarnings("all")
327    public String getPercentage() {
328      return this.percentage;
329    }
330
331    @java.lang.SuppressWarnings("all")
332    public Integer getCallCount() {
333      return this.callCount;
334    }
335
336    @java.lang.SuppressWarnings("all")
337    public Integer getTotalTime() {
338      return this.totalTime;
339    }
340
341    @java.lang.SuppressWarnings("all")
342    public Integer getTotalCputime() {
343      return this.totalCputime;
344    }
345
346    @java.lang.SuppressWarnings("all")
347    public Double getAccIdUtilPct() {
348      return this.accIdUtilPct;
349    }
350  }
351
352
353  public static class DebugHeaderInfoFactory implements Serializable {
354    private String debug;
355    private String rev;
356    private String traceId;
357    private Version version;
358    private String appUsage;
359    private String pageUsage;
360    private String adAccountUsage;
361    private String businessUseCaseUsage;
362
363    public static DebugHeaderInfoFactory create() {
364      return new DebugHeaderInfoFactory();
365    }
366
367    public DebugHeaderInfoFactory setVersion(Version version) {
368      this.version = version;
369      return this;
370    }
371
372    public DebugHeaderInfoFactory setDebug(String debug) {
373      this.debug = debug;
374      return this;
375    }
376
377    public DebugHeaderInfoFactory setRev(String rev) {
378      this.rev = rev;
379      return this;
380    }
381
382    public DebugHeaderInfoFactory setTraceId(String traceId) {
383      this.traceId = traceId;
384      return this;
385    }
386
387    public DebugHeaderInfoFactory setAppUsage(String appUsage) {
388      this.appUsage = appUsage;
389      return this;
390    }
391
392    public DebugHeaderInfoFactory setPageUsage(String pageUsage) {
393      this.pageUsage = pageUsage;
394      return this;
395    }
396
397    public DebugHeaderInfoFactory setAdAccountUsage(String adAccountUsage) {
398      this.adAccountUsage = adAccountUsage;
399      return this;
400    }
401
402    public DebugHeaderInfoFactory setBusinessUseCaseUsage(String businessUseCaseUsage) {
403      this.businessUseCaseUsage = businessUseCaseUsage;
404      return this;
405    }
406
407    public DebugHeaderInfo build() {
408      return new DebugHeaderInfo(this.debug, this.rev, this.traceId, this.version, this.appUsage, this.pageUsage, this.adAccountUsage, this.businessUseCaseUsage);
409    }
410  }
411}