Skip to content

Commit 558a796

Browse files
committed
refactor: reorganize SheetClientFactory and DataValue classes, update tests, and remove unused files
1 parent 5220bf8 commit 558a796

File tree

8 files changed

+133
-10
lines changed

8 files changed

+133
-10
lines changed

src/main/java/io/github/codenilson/gsapi_core/GsAPI.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,38 @@
55
import java.util.List;
66
import java.util.Optional;
77

8+
import io.github.codenilson.gsapi_core.client.GoogleSheetsClient;
89
import io.github.codenilson.gsapi_core.errors.GSAPIError;
10+
import io.github.codenilson.gsapi_core.models.DataValue;
911

12+
/**
13+
* Main class for interacting with the Google Sheets API.
14+
*/
1015
public class GsAPI {
1116

1217
private GoogleSheetsClient client;
1318

19+
/**
20+
* Constructs a new GsAPI with the specified Google Sheets client.
21+
*
22+
* @param client The Google Sheets client used to interact with the API.
23+
* @see GoogleSheetsClient
24+
*/
1425
public GsAPI(GoogleSheetsClient client) {
1526
this.client = client;
1627
}
1728

29+
/**
30+
* Retrieves data from the specified range in the Google Sheets.
31+
*
32+
* @param request The request containing the spreadsheet ID and range.
33+
* @return An Optional containing a list of {@link DataValue} objects
34+
* representing the data in the specified range,
35+
* or an empty Optional if no data is found.
36+
* @throws GSAPIError If an error occurs while retrieving the data.
37+
* @see SheetRequest
38+
* @see DataValue
39+
*/
1840
public Optional<List<DataValue>> retrieveData(SheetRequest request) {
1941
try {
2042
List<List<Object>> values = client.fetchValues(request);
@@ -33,6 +55,15 @@ public Optional<List<DataValue>> retrieveData(SheetRequest request) {
3355
}
3456
}
3557

58+
/**
59+
* Adds data to the specified range in the Google Sheets.
60+
*
61+
* @param request The request containing the spreadsheet ID and range.
62+
* @param values The list of {@link DataValue} objects to be added.
63+
* @throws GSAPIError If an error occurs while appending the data.
64+
* @see SheetRequest
65+
* @see DataValue
66+
*/
3667
public void addData(SheetRequest request, List<DataValue> values) {
3768
try {
3869
List<List<Object>> data = new ArrayList<>();
@@ -46,17 +77,32 @@ public void addData(SheetRequest request, List<DataValue> values) {
4677
}
4778
}
4879

80+
/**
81+
* Modifies data in the specified range in the Google Sheets.
82+
*
83+
* @param request The request containing the spreadsheet ID and range.
84+
* @param value The {@link DataValue} object containing the new data.
85+
* @throws GSAPIError If an error occurs while updating the data.
86+
* @see SheetRequest
87+
* @see DataValue
88+
*/
4989
public void modifyData(SheetRequest request, DataValue value) {
5090
try {
5191
List<List<Object>> data = new ArrayList<>();
5292
data.add(value.getValues());
5393
client.overwriteValues(request, data);
5494
} catch (IOException e) {
55-
throw new GSAPIError("An error occurred while updating the sheet data for range: " + request.getRange(),
56-
e);
95+
throw new GSAPIError("An error occurred while updating the sheet data for range: " + request.getRange(), e);
5796
}
5897
}
5998

99+
/**
100+
* Clears the specified range in the Google Sheets.
101+
*
102+
* @param request The request containing the spreadsheet ID and range.
103+
* @throws GSAPIError If an error occurs while clearing the data.
104+
* @see SheetRequest
105+
*/
60106
public void resetRange(SheetRequest request) {
61107
try {
62108
client.clearRange(request);
@@ -65,4 +111,4 @@ public void resetRange(SheetRequest request) {
65111
}
66112
}
67113

68-
}
114+
}
Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,70 @@
11
package io.github.codenilson.gsapi_core;
22

3+
/**
4+
* Represents a request to interact with a specific range in a Google Sheets
5+
* spreadsheet.
6+
*/
37
public class SheetRequest {
48
private String spreadsheetId;
59
private String range;
610

11+
/**
12+
* Constructs a new SheetRequest with the specified spreadsheet ID and range.
13+
*
14+
* @param spreadsheetId The ID of the Google Sheets spreadsheet.
15+
* @param range The range within the spreadsheet to interact with.
16+
*/
717
public SheetRequest(String spreadsheetId, String range) {
818
this.spreadsheetId = spreadsheetId;
919
this.range = range;
1020
}
1121

22+
/**
23+
* Gets the ID of the Google Sheets spreadsheet.
24+
*
25+
* @return The spreadsheet ID.
26+
*/
1227
public String getSpreadsheetId() {
1328
return spreadsheetId;
1429
}
1530

31+
/**
32+
* Gets the range within the Google Sheets spreadsheet.
33+
*
34+
* @return The range.
35+
*/
1636
public String getRange() {
1737
return range;
1838
}
1939

40+
/**
41+
* Sets the ID of the Google Sheets spreadsheet.
42+
*
43+
* @param spreadsheetId The spreadsheet ID to set.
44+
*/
2045
public void setSpreadsheetId(String spreadsheetId) {
2146
this.spreadsheetId = spreadsheetId;
2247
}
2348

49+
/**
50+
* Sets the range within the Google Sheets spreadsheet.
51+
*
52+
* @param range The range to set.
53+
*/
2454
public void setRange(String range) {
2555
this.range = range;
2656
}
2757

58+
/**
59+
* Returns a string representation of the SheetRequest.
60+
*
61+
* @return A string containing the spreadsheet ID and range.
62+
*/
2863
@Override
2964
public String toString() {
3065
return "SheetRequest{" +
3166
"spreadsheetId='" + spreadsheetId + '\'' +
3267
", range='" + range + '\'' +
3368
'}';
3469
}
35-
}
70+
}

src/main/java/io/github/codenilson/gsapi_core/GoogleSheetsClient.java renamed to src/main/java/io/github/codenilson/gsapi_core/client/GoogleSheetsClient.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.codenilson.gsapi_core;
1+
package io.github.codenilson.gsapi_core.client;
22

33
import java.io.IOException;
44
import java.util.List;
@@ -7,21 +7,47 @@
77
import com.google.api.services.sheets.v4.model.ClearValuesRequest;
88
import com.google.api.services.sheets.v4.model.ValueRange;
99

10+
import io.github.codenilson.gsapi_core.SheetRequest;
11+
12+
/**
13+
* Client class for interacting with the Google Sheets API.
14+
*/
1015
public class GoogleSheetsClient {
1116

1217
private Sheets service;
1318

19+
/**
20+
* Constructs a new GoogleSheetsClient with the specified Sheets service.
21+
*
22+
* @param service The Sheets service used to interact with the Google Sheets
23+
* API.
24+
*/
1425
public GoogleSheetsClient(Sheets service) {
1526
this.service = service;
1627
}
1728

29+
/**
30+
* Fetches values from the specified range in the Google Sheets.
31+
*
32+
* @param request The request containing the spreadsheet ID and range.
33+
* @return A list of lists of objects representing the values in the specified
34+
* range.
35+
* @throws IOException If an I/O error occurs while fetching the values.
36+
*/
1837
public List<List<Object>> fetchValues(SheetRequest request) throws IOException {
1938
return service.spreadsheets().values()
2039
.get(request.getSpreadsheetId(), request.getRange())
2140
.execute()
2241
.getValues();
2342
}
2443

44+
/**
45+
* Appends values to the specified range in the Google Sheets.
46+
*
47+
* @param request The request containing the spreadsheet ID and range.
48+
* @param values The values to be appended.
49+
* @throws IOException If an I/O error occurs while appending the values.
50+
*/
2551
public void appendValues(SheetRequest request, List<List<Object>> values) throws IOException {
2652
ValueRange valueRange = new ValueRange().setValues(values);
2753
service.spreadsheets().values()
@@ -31,6 +57,13 @@ public void appendValues(SheetRequest request, List<List<Object>> values) throws
3157
.execute();
3258
}
3359

60+
/**
61+
* Overwrites values in the specified range in the Google Sheets.
62+
*
63+
* @param request The request containing the spreadsheet ID and range.
64+
* @param values The values to overwrite the existing values.
65+
* @throws IOException If an I/O error occurs while overwriting the values.
66+
*/
3467
public void overwriteValues(SheetRequest request, List<List<Object>> values) throws IOException {
3568
ValueRange valueRange = new ValueRange().setValues(values);
3669
service.spreadsheets().values()
@@ -39,9 +72,15 @@ public void overwriteValues(SheetRequest request, List<List<Object>> values) thr
3972
.execute();
4073
}
4174

75+
/**
76+
* Clears the specified range in the Google Sheets.
77+
*
78+
* @param request The request containing the spreadsheet ID and range.
79+
* @throws IOException If an I/O error occurs while clearing the range.
80+
*/
4281
public void clearRange(SheetRequest request) throws IOException {
4382
service.spreadsheets().values()
4483
.clear(request.getSpreadsheetId(), request.getRange(), new ClearValuesRequest())
4584
.execute();
4685
}
47-
}
86+
}

src/main/java/io/github/codenilson/gsapi_core/SheetClientFactory.java renamed to src/main/java/io/github/codenilson/gsapi_core/client/SheetClientFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.codenilson.gsapi_core;
1+
package io.github.codenilson.gsapi_core.client;
22

33
import java.io.IOException;
44
import java.security.GeneralSecurityException;
@@ -35,4 +35,4 @@ public static Sheets getSheetsService(HttpRequestInitializer initializer, String
3535
.setApplicationName(applicationName)
3636
.build();
3737
}
38-
}
38+
}

src/main/java/io/github/codenilson/gsapi_core/DataValue.java renamed to src/main/java/io/github/codenilson/gsapi_core/models/DataValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.codenilson.gsapi_core;
1+
package io.github.codenilson.gsapi_core.models;
22

33
import java.util.ArrayList;
44
import java.util.List;

src/main/java/io/github/codenilson/gsapi_core/Authenticator.java renamed to src/main/java/io/github/codenilson/gsapi_core/utils/Authenticator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.codenilson.gsapi_core;
1+
package io.github.codenilson.gsapi_core.utils;
22

33
import java.io.IOException;
44
import java.io.InputStream;

src/test/java/io/github/codenilson/gsapi_core/AuthenticatorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.auth.oauth2.GoogleCredentials;
1919

2020
import io.github.codenilson.gsapi_core.errors.GSAPIError;
21+
import io.github.codenilson.gsapi_core.utils.Authenticator;
2122

2223
class AuthenticatorTest {
2324

src/test/java/io/github/codenilson/gsapi_core/DataValueTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.junit.jupiter.api.BeforeEach;
88
import org.junit.jupiter.api.Test;
99

10+
import io.github.codenilson.gsapi_core.models.DataValue;
11+
1012
public class DataValueTest {
1113

1214
private DataValue dataValue;

0 commit comments

Comments
 (0)