Coverage Report - com.jcabi.github.RtGithub
 
Classes in this File Line Coverage Branch Coverage Complexity
RtGithub
70%
17/24
50%
5/10
1
RtGithub$AjcClosure1
100%
1/1
N/A
1
RtGithub$AjcClosure11
100%
1/1
N/A
1
RtGithub$AjcClosure13
100%
1/1
N/A
1
RtGithub$AjcClosure15
0%
0/1
N/A
1
RtGithub$AjcClosure17
100%
1/1
N/A
1
RtGithub$AjcClosure19
100%
1/1
N/A
1
RtGithub$AjcClosure3
100%
1/1
N/A
1
RtGithub$AjcClosure5
100%
1/1
N/A
1
RtGithub$AjcClosure7
100%
1/1
N/A
1
RtGithub$AjcClosure9
0%
0/1
N/A
1
 
 1  2
 /**
 2  
  * Copyright (c) 2013-2015, 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 com.jcabi.http.Request;
 35  
 import com.jcabi.http.request.ApacheRequest;
 36  
 import com.jcabi.http.response.JsonResponse;
 37  
 import com.jcabi.manifests.Manifests;
 38  
 import java.io.IOException;
 39  
 import javax.json.JsonObject;
 40  
 import javax.validation.constraints.NotNull;
 41  
 import javax.ws.rs.core.HttpHeaders;
 42  
 import javax.ws.rs.core.MediaType;
 43  
 import javax.xml.bind.DatatypeConverter;
 44  
 import lombok.EqualsAndHashCode;
 45  
 import lombok.ToString;
 46  
 import org.apache.commons.io.Charsets;
 47  
 
 48  
 /**
 49  
  * Github client, starting point to the entire library.
 50  
  *
 51  
  * <p>This is how you start communicating with Github API:
 52  
  *
 53  
  * <pre> Github github = new RtGithub(oauthKey);
 54  
  * Repo repo = github.repos().get(
 55  
  *     new Coordinates.Simple("jcabi/jcabi-github")
 56  
  * );
 57  
  * Issues issues = repo.issues();
 58  
  * Issue issue = issues.create("issue title", "issue body");
 59  
  * issue.comments().post("issue comment");</pre>
 60  
  *
 61  
  * <p>It is strongly recommended to use
 62  
  * {@link com.jcabi.http.wire.RetryWire} to avoid
 63  
  * accidental I/O exceptions:
 64  
  *
 65  
  * <pre> Github github = new RtGithub(
 66  
  *   new RtGithub(oauthKey).entry().through(RetryWire.class)
 67  
  * );</pre>
 68  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 69  
  * @version $Id$
 70  
  * @since 0.1
 71  
  * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
 72  
  */
 73  
 @Immutable
 74  
 @Loggable(Loggable.DEBUG)
 75  0
 @ToString
 76  2
 @EqualsAndHashCode(of = "request")
 77  
 public final class RtGithub implements Github {
 78  
 
 79  
     /**
 80  
      * Version of us.
 81  
      */
 82  1
     private static final String USER_AGENT = String.format(
 83  
         "jcabi-github %s %s %s",
 84  
         Manifests.read("JCabi-Version"),
 85  
         Manifests.read("JCabi-Build"),
 86  
         Manifests.read("JCabi-Date")
 87  
     );
 88  
 
 89  
     /**
 90  
      * Default request to start with.
 91  
      */
 92  1
     private static final Request REQUEST =
 93  
         new ApacheRequest("https://api.github.com")
 94  
             .header(HttpHeaders.USER_AGENT, RtGithub.USER_AGENT)
 95  
             .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
 96  
             .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
 97  
 
 98  
     /**
 99  
      * REST request.
 100  
      */
 101  
     private final transient Request request;
 102  
 
 103  
     /**
 104  
      * Public ctor, for anonymous access to Github.
 105  
      * @since 0.4
 106  
      */
 107  
     public RtGithub() {
 108  10
         this(RtGithub.REQUEST);
 109  10
     }
 110  
 
 111  
     /**
 112  
      * Public ctor, for HTTP Basic Authentication.
 113  
      * @param user User name
 114  
      * @param pwd Password
 115  
      * @since 0.4
 116  
      */
 117  
     public RtGithub(
 118  
         @NotNull(message = "user name can't be NULL") final String user,
 119  
         @NotNull(message = "password can't be NULL") final String pwd) {
 120  0
         this(
 121  
             RtGithub.REQUEST.header(
 122  
                 HttpHeaders.AUTHORIZATION,
 123  
                 String.format(
 124  
                     "Basic %s",
 125  
                     DatatypeConverter.printBase64Binary(
 126  
                         String.format("%s:%s", user, pwd)
 127  
                             .getBytes(Charsets.UTF_8)
 128  
                     )
 129  
                 )
 130  
             )
 131  
         );
 132  0
     }
 133  
 
 134  
     /**
 135  
      * Public ctor, for authentication with OAuth2 token.
 136  
      * @param token OAuth token
 137  
      */
 138  
     public RtGithub(
 139  
         @NotNull(message = "token can't be NULL") final String token) {
 140  0
         this(
 141  
             RtGithub.REQUEST.header(
 142  
                 HttpHeaders.AUTHORIZATION,
 143  
                 String.format("token %s", token)
 144  
             )
 145  
         );
 146  0
     }
 147  
 
 148  
     /**
 149  
      * Public ctor, with a custom request.
 150  
      * @param req Request to start from
 151  
      * @since 0.4
 152  
      */
 153  
     public RtGithub(
 154  27
         @NotNull(message = "request can't be NULL") final Request req) {
 155  27
         this.request = req;
 156  27
     }
 157  
 
 158  
     @Override
 159  
     @NotNull(message = "request can't be NULL")
 160  
     public Request entry() {
 161  6
         return this.request;
 162  
     }
 163  
 
 164  
     @Override
 165  
     @NotNull(message = "repos is never NULL")
 166  
     public Repos repos() {
 167  30
         return new RtRepos(this, this.request);
 168  
     }
 169  
 
 170  
     @Override
 171  
     @NotNull(message = "gists are never NULL")
 172  
     public Gists gists() {
 173  2
         return new RtGists(this, this.request);
 174  
     }
 175  
 
 176  
     @Override
 177  
     @NotNull(message = "users are never NULL")
 178  
     public Users users() {
 179  4
         return new RtUsers(this, this.request);
 180  
     }
 181  
 
 182  
     @Override
 183  
     @NotNull(message = "limmits can't be NULL")
 184  
     public Limits limits() {
 185  0
         return new RtLimits(this, this.request);
 186  
     }
 187  
 
 188  
     @Override
 189  
     @NotNull(message = "search is never NULL")
 190  
     public Search search() {
 191  8
         return new RtSearch(this, this.request);
 192  
     }
 193  
 
 194  
     @Override
 195  
     @NotNull(message = "JSON is never NULL")
 196  
     public JsonObject meta() throws IOException {
 197  4
         return this.request.uri().path("meta").back().fetch()
 198  
             .as(JsonResponse.class)
 199  
             .json().readObject();
 200  
     }
 201  
 
 202  
     @Override
 203  
     @NotNull(message = "JSON is never NULL")
 204  
     public JsonObject emojis() throws IOException {
 205  0
         return this.request.uri().path("emojis").back().fetch()
 206  
             .as(JsonResponse.class)
 207  
             .json().readObject();
 208  
     }
 209  
 
 210  
     @Override
 211  
     @NotNull(message = "GitIgnores is never NULL")
 212  
     public Gitignores gitignores() throws IOException {
 213  2
         return new RtGitignores(this);
 214  
     }
 215  
 
 216  
     @Override
 217  
     @NotNull(message = "Markdown is never NULL")
 218  
     public Markdown markdown() {
 219  2
         return new RtMarkdown(this, this.request);
 220  
     }
 221  
 
 222  
 }