Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RtTrees |
|
| 1.0;1 | ||||
RtTrees$AjcClosure1 |
|
| 1.0;1 | ||||
RtTrees$AjcClosure3 |
|
| 1.0;1 | ||||
RtTrees$AjcClosure5 |
|
| 1.0;1 | ||||
RtTrees$AjcClosure7 |
|
| 1.0;1 |
1 | 0 | /** |
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.response.JsonResponse; | |
36 | import com.jcabi.http.response.RestResponse; | |
37 | import java.io.IOException; | |
38 | import java.net.HttpURLConnection; | |
39 | import javax.json.JsonObject; | |
40 | import lombok.EqualsAndHashCode; | |
41 | ||
42 | /** | |
43 | * Github trees. | |
44 | * @version $Id: d005d05b09dc80d806fd4bfce6b511b7243bac0f $ | |
45 | * @author Alexander Lukashevich (sanai56967@gmail.com) | |
46 | */ | |
47 | @Immutable | |
48 | @Loggable(Loggable.DEBUG) | |
49 | 0 | @EqualsAndHashCode(of = { "request", "owner", "entry" }) |
50 | final class RtTrees implements Trees { | |
51 | ||
52 | /** | |
53 | * RESTful API entry point. | |
54 | */ | |
55 | private final transient Request entry; | |
56 | ||
57 | /** | |
58 | * Repository. | |
59 | */ | |
60 | private final transient Repo owner; | |
61 | ||
62 | /** | |
63 | * RESTful request. | |
64 | */ | |
65 | private final transient Request request; | |
66 | ||
67 | /** | |
68 | * Public ctor. | |
69 | * @param req Entry point of API | |
70 | * @param repo Repository | |
71 | */ | |
72 | 3 | RtTrees(final Request req, final Repo repo) { |
73 | 3 | this.entry = req; |
74 | 3 | this.owner = repo; |
75 | 3 | this.request = req.uri() |
76 | 3 | .path("/repos") |
77 | 3 | .path(repo.coordinates().user()) |
78 | 3 | .path(repo.coordinates().repo()) |
79 | 3 | .path("/git") |
80 | 3 | .path("/trees") |
81 | 3 | .back(); |
82 | 3 | } |
83 | ||
84 | @Override | |
85 | public Repo repo() { | |
86 | 0 | return this.owner; |
87 | } | |
88 | ||
89 | @Override | |
90 | public Tree get(final String sha) { | |
91 | 6 | return new RtTree(this.entry, this.owner, sha); |
92 | } | |
93 | ||
94 | @Override | |
95 | public Tree getRec( | |
96 | final String sha | |
97 | ) { | |
98 | 3 | return new RtTree( |
99 | 1 | this.entry.uri().queryParam("recursive", "1").back(), |
100 | this.owner, sha | |
101 | ); | |
102 | } | |
103 | ||
104 | @Override | |
105 | public Tree create( | |
106 | final JsonObject params | |
107 | ) throws IOException { | |
108 | 3 | return this.get( |
109 | 1 | this.request.method(Request.POST) |
110 | 1 | .body().set(params).back() |
111 | 1 | .fetch().as(RestResponse.class) |
112 | 1 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
113 | 1 | .as(JsonResponse.class) |
114 | 1 | .json().readObject().getString("sha") |
115 | ); | |
116 | } | |
117 | } |