• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

smartsheet / smartsheet-java-sdk / #41

24 Aug 2023 04:59PM UTC coverage: 50.458% (+0.01%) from 50.444%
#41

push

github-actions

web-flow
Fix Checkstyle Violations in "Impl" Classes (#53)

241 of 241 new or added lines in 32 files covered. (100.0%)

3417 of 6772 relevant lines covered (50.46%)

0.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/com/smartsheet/api/internal/PassthroughResourcesImpl.java
1
package com.smartsheet.api.internal;
2

3
/*
4
 * #[license]
5
 * Smartsheet SDK for Java
6
 * %%
7
 * Copyright (C) 2023 Smartsheet
8
 * %%
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *      http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 * %[license]
21
 */
22

23
import com.smartsheet.api.AuthorizationException;
24
import com.smartsheet.api.InvalidRequestException;
25
import com.smartsheet.api.PassthroughResources;
26
import com.smartsheet.api.ResourceNotFoundException;
27
import com.smartsheet.api.ServiceUnavailableException;
28
import com.smartsheet.api.SmartsheetException;
29
import com.smartsheet.api.internal.http.HttpEntity;
30
import com.smartsheet.api.internal.http.HttpMethod;
31
import com.smartsheet.api.internal.http.HttpRequest;
32
import com.smartsheet.api.internal.http.HttpResponse;
33
import com.smartsheet.api.internal.util.QueryUtil;
34
import com.smartsheet.api.internal.util.Util;
35

36
import java.io.BufferedReader;
37
import java.io.ByteArrayInputStream;
38
import java.io.IOException;
39
import java.io.InputStreamReader;
40
import java.util.Map;
41

42
public class PassthroughResourcesImpl extends AbstractResources implements PassthroughResources {
43

44
    /**
45
     * Constructor.
46
     * <p>
47
     * Exceptions: - IllegalArgumentException : if any argument is null
48
     *
49
     * @param smartsheet the smartsheet
50
     */
51
    public PassthroughResourcesImpl(SmartsheetImpl smartsheet) {
52
        super(smartsheet);
×
53
    }
×
54

55
    /**
56
     * Issue an HTTP GET request.
57
     *
58
     * @param endpoint the API endpoint
59
     * @param parameters optional list of resource parameters
60
     * @return a JSON response string
61
     * @throws IllegalArgumentException if any argument is null or empty string
62
     * @throws InvalidRequestException if there is any problem with the REST API request
63
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
64
     * @throws ResourceNotFoundException if the resource cannot be found
65
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
66
     * @throws SmartsheetException if there is any other error during the operation
67
     */
68
    public String getRequest(String endpoint, Map<String, Object> parameters) throws SmartsheetException {
69
        return passthroughRequest(HttpMethod.GET, endpoint, null, parameters);
×
70
    }
71

72
    /**
73
     * Issue an HTTP POST request.
74
     *
75
     * @param endpoint the API endpoint
76
     * @param payload a JSON payload string
77
     * @param parameters optional list of resource parameters
78
     * @return a JSON response string
79
     * @throws IllegalArgumentException if any argument is null or empty string
80
     * @throws InvalidRequestException if there is any problem with the REST API request
81
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
82
     * @throws ResourceNotFoundException if the resource cannot be found
83
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
84
     * @throws SmartsheetException if there is any other error during the operation
85
     */
86
    public String postRequest(String endpoint, String payload, Map<String, Object> parameters) throws SmartsheetException {
87
        Util.throwIfNull(payload);
×
88
        return passthroughRequest(HttpMethod.POST, endpoint, payload, parameters);
×
89
    }
90

91
    /**
92
     * Issue an HTTP PUT request.
93
     *
94
     * @param endpoint the API endpoint
95
     * @param payload a JSON payload string
96
     * @param parameters optional list of resource parameters
97
     * @return a JSON response string
98
     * @throws IllegalArgumentException if any argument is null or empty string
99
     * @throws InvalidRequestException if there is any problem with the REST API request
100
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
101
     * @throws ResourceNotFoundException if the resource cannot be found
102
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
103
     * @throws SmartsheetException if there is any other error during the operation
104
     */
105
    public String putRequest(String endpoint, String payload, Map<String, Object> parameters) throws SmartsheetException {
106
        Util.throwIfNull(payload);
×
107
        return passthroughRequest(HttpMethod.PUT, endpoint, payload, parameters);
×
108
    }
109

110
    /**
111
     * Issue an HTTP DELETE request.
112
     *
113
     * @param endpoint the API endpoint
114
     * @return a JSON response string
115
     * @throws IllegalArgumentException if any argument is null or empty string
116
     * @throws InvalidRequestException if there is any problem with the REST API request
117
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
118
     * @throws ResourceNotFoundException if the resource cannot be found
119
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
120
     * @throws SmartsheetException if there is any other error during the operation
121
     */
122
    public String deleteRequest(String endpoint) throws SmartsheetException {
123
        return passthroughRequest(HttpMethod.DELETE, endpoint, null, null);
×
124
    }
125

126
    /**
127
     * Passthrough request
128
     *
129
     * @param method HTTP method
130
     * @param endpoint the API endpoint (required)
131
     * @param payload optional JSON payload
132
     * @param parameters optional list of resource parameters
133
     * @return the result string
134
     */
135
    private String passthroughRequest(
136
            HttpMethod method,
137
            String endpoint,
138
            String payload,
139
            Map<String, Object> parameters
140
    ) throws SmartsheetException {
141
        Util.throwIfNull(endpoint);
×
142
        Util.throwIfEmpty(endpoint);
×
143

144
        if (parameters != null) {
×
145
            endpoint += QueryUtil.generateUrl(null, parameters);
×
146
        }
147

148
        HttpRequest request = createHttpRequest(smartsheet.getBaseURI().resolve(endpoint), method);
×
149

150
        if (payload != null) {
×
151
            HttpEntity entity = new HttpEntity();
×
152
            entity.setContentType("application/json");
×
153
            entity.setContent(new ByteArrayInputStream(payload.getBytes()));
×
154
            entity.setContentLength(payload.getBytes().length);
×
155
            request.setEntity(entity);
×
156
        }
157

158
        String res = null;
×
159
        try {
160
            HttpResponse response = this.smartsheet.getHttpClient().request(request);
×
161
            switch (response.getStatusCode()) {
×
162
                case 200:
163
                    String readLine;
164
                    try {
165
                        BufferedReader br;
166
                        StringBuilder sb = new StringBuilder();
×
167
                        br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
×
168
                        while ((readLine = br.readLine()) != null) {
×
169
                            sb.append(readLine);
×
170
                        }
171
                        br.close();
×
172
                        res = sb.toString();
×
173
                    } catch (IOException e) {
×
174
                        res = null;
×
175
                    }
×
176
                    break;
×
177
                default:
178
                    handleError(response);
×
179
            }
180
        } finally {
181
            smartsheet.getHttpClient().releaseConnection();
×
182
        }
183
        return res;
×
184
    }
185
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc