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