View Javadoc
1   /**
2    * Copyright (c) 2013-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github;
31  
32  import com.jcabi.aspects.Immutable;
33  import com.jcabi.aspects.Loggable;
34  import java.io.IOException;
35  import java.util.Date;
36  import java.util.concurrent.TimeUnit;
37  import javax.json.Json;
38  import javax.json.JsonObject;
39  import lombok.EqualsAndHashCode;
40  import lombok.ToString;
41  
42  /**
43   * Github Rate Limit API, one resource limit.
44   *
45   * @author Yegor Bugayenko (yegor256@gmail.com)
46   * @version $Id: 476236a31c8911f81f0c54bd200535d826430383 $
47   * @since 0.6
48   * @see <a href="https://developer.github.com/v3/rate_limit/">Rate Limit API</a>
49   * @checkstyle MultipleStringLiterals (500 lines)
50   */
51  @Immutable
52  public interface Limit extends JsonReadable {
53  
54      /**
55       * Github we're in.
56       * @return Github
57       */
58      Github github();
59  
60      /**
61       * Smart limits with extra features.
62       */
63      @Immutable
64      @ToString
65      @Loggable(Loggable.DEBUG)
66      @EqualsAndHashCode(of = "origin")
67      final class Smart implements Limit {
68          /**
69           * Encapsulated limit.
70           */
71          private final transient Limit origin;
72          /**
73           * Public ctor.
74           * @param limit Limit
75           */
76          public Smart(
77              final Limit limit
78          ) {
79              this.origin = limit;
80          }
81          /**
82           * Limit of number of requests.
83           * @return Number of requests you can make in total
84           * @throws IOException If it fails
85           */
86          public int limit() throws IOException {
87              return new SmartJson(this.origin).number("limit");
88          }
89          /**
90           * Remaining number of requests.
91           * @return Number of requests you can still make
92           * @throws IOException If it fails
93           */
94          public int remaining() throws IOException {
95              return new SmartJson(this.origin).number("remaining");
96          }
97          /**
98           * When will the limit be reset.
99           * @return Date when this will happen
100          * @throws IOException If it fails
101          */
102         public Date reset() throws IOException {
103             return new Date(
104                 TimeUnit.MILLISECONDS.convert(
105                     (long) new SmartJson(this.origin).number("reset"),
106                     TimeUnit.SECONDS
107                 )
108             );
109         }
110         @Override
111         public JsonObject json() throws IOException {
112             return this.origin.json();
113         }
114         @Override
115         public Github github() {
116             return this.origin.github();
117         }
118     }
119 
120     /**
121      * Throttled Limit.
122      * @since 0.6
123      */
124     @Immutable
125     @ToString
126     @Loggable(Loggable.DEBUG)
127     @EqualsAndHashCode(of = { "origin", "jsn" })
128     final class Throttled implements Limit {
129         /**
130          * Original.
131          */
132         private final transient Limit origin;
133         /**
134          * SmartJson object for convenient JSON parsing.
135          */
136         private final transient SmartJson jsn;
137         /**
138          * Maximum allowed, instead of default 5000.
139          */
140         private final transient int max;
141         /**
142          * Public ctor.
143          * @param limit Original limit
144          * @param allowed Maximum allowed
145          */
146         public Throttled(
147             final Limit limit,
148             final int allowed
149         ) {
150             this.origin = limit;
151             this.max = allowed;
152             this.jsn = new SmartJson(limit);
153         }
154         @Override
155         public JsonObject json() throws IOException {
156             final int limit = new SmartJson(this.origin).number("limit");
157             final int remaining = this.max - (
158                 limit - new SmartJson(this.origin).number("remaining")
159                 );
160             return Json.createObjectBuilder()
161                 .add("limit", limit)
162                 .add("remaining", remaining)
163                 .add("reset", new SmartJson(this.origin).number("reset"))
164                 .build();
165         }
166         @Override
167         public Github github() {
168             return this.origin.github();
169         }
170     }
171 
172 }