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.net.URL;
36  import javax.json.Json;
37  import javax.json.JsonObject;
38  import lombok.EqualsAndHashCode;
39  import lombok.ToString;
40  
41  /**
42   * Github deploy key.
43   *
44   * @author Andres Candal (andres.candal@rollasolution.com)
45   * @version $Id: 7bf69b4e721c6bed10d137ece1c58d418f5987df $
46   * @since 0.8
47   * @see <a href="https://developer.github.com/v3/repos/keys/">Deploy Keys API</a>
48   */
49  @Immutable
50  @SuppressWarnings("PMD.TooManyMethods")
51  public interface DeployKey extends JsonReadable, JsonPatchable {
52  
53      /**
54       * Get id of a deploy key.
55       * @return Id
56       */
57      int number();
58  
59      /**
60       * Delete a deploy key.
61       * @throws java.io.IOException If there is any I/O problem
62       * @see <a href="https://developer.github.com/v3/repos/keys/#delete">Remove a deploy key</a>
63       */
64      void remove() throws IOException;
65  
66      /**
67       * Smart DeployKey with extra features.
68       * @checkstyle MultipleStringLiterals (500 lines)
69       */
70      @Immutable
71      @ToString
72      @Loggable(Loggable.DEBUG)
73      @EqualsAndHashCode(of = { "key", "jsn" })
74      final class Smart implements DeployKey {
75  
76          /**
77           * Encapsulated deploy key.
78           */
79          private final transient DeployKey key;
80  
81          /**
82           * SmartJson object for convenient JSON parsing.
83           */
84          private final transient SmartJson jsn;
85  
86          /**
87           * Public ctor.
88           * @param dkey Deploy key
89           */
90          public Smart(final DeployKey dkey) {
91              this.key = dkey;
92              this.jsn = new SmartJson(dkey);
93          }
94  
95          /**
96           * Get its key value.
97           * @return Value of deploy key
98           * @throws IOException If there is any I/O problem
99           */
100         public String key() throws IOException {
101             return this.jsn.text("key");
102         }
103 
104         /**
105          * Change its value.
106          * @param value Title of deploy key
107          * @throws IOException If there is any I/O problem
108          */
109         public void key(final String value) throws IOException {
110             this.key.patch(
111                 Json.createObjectBuilder().add("key", value).build()
112             );
113         }
114 
115         /**
116          * Get its URL.
117          * @return URL of deploy key
118          * @throws IOException If there is any I/O problem
119          */
120         public URL url() throws IOException {
121             return new URL(this.jsn.text("url"));
122         }
123 
124         /**
125          * Get its title.
126          * @return Title of deploy key
127          * @throws IOException If there is any I/O problem
128          */
129         public String title() throws IOException {
130             return this.jsn.text("title");
131         }
132 
133         /**
134          * Change its title.
135          * @param text Title of deploy key
136          * @throws IOException If there is any I/O problem
137          */
138         public void title(final String text) throws IOException {
139             this.key.patch(
140                 Json.createObjectBuilder().add("title", text).build()
141             );
142         }
143 
144         @Override
145         public JsonObject json() throws IOException {
146             return this.key.json();
147         }
148 
149         @Override
150         public void patch(final JsonObject json) throws IOException {
151             this.key.patch(json);
152         }
153 
154         @Override
155         public int number() {
156             return this.key.number();
157         }
158 
159         @Override
160         public void remove() throws IOException {
161             this.key.remove();
162         }
163     }
164 
165 }