Skip to content

Commit 3ec7b3b

Browse files
zacmostmoskovitch
and
tmoskovitch
authored
Get response fields by defining the excluded fields. (#32)
* Get the required response fields by define the excluded fields. * Get the required response fields by define the excluded fields. Co-authored-by: tmoskovitch <[email protected]>
1 parent b17b44e commit 3ec7b3b

18 files changed

+945
-54
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Twitter API v2 available endpoints
2626
- [Authentication](#authentication)
2727
- [Auto Refresh Token](#auto-refresh-token)
2828
- [Rate limits retry mechanism](#rate-limits-retry-mechanism)
29+
- [Get all fields](#get-all-fields)
2930
- [Documentation for API Endpoints](#documentation-for-api-endpoints)
3031
- [Documentation for Models](#documentation-for-models)
3132

@@ -239,6 +240,32 @@ In order to use the retry mechanism call the APIs with an additional parameter `
239240
Read more about Filtered stream and [rate limits](https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/handling-disconnections)
240241

241242

243+
## Get all fields
244+
245+
246+
The Twitter API v2 endpoints are equipped with a set of parameters called [fields](https://developer.twitter.com/en/docs/twitter-api/fields), which allows you to select just the data that you want from each of the objects in your endpoint response.
247+
248+
When you need to get many fields in the response object it can be a bit gruelling to define all the needed fields.
249+
In order to make it easier, you can use the `excludeInputFields` approach. Instead of selecting the data that you want to get, you define which fields should be excluded from the response object.
250+
Using `excludeInputFields()` will change the behaviour of the execution, in this case all response fields will be returned besides the ones that were sent as input parameters.
251+
252+
253+
```java
254+
Set<String> tweetFields = new HashSet<>();
255+
tweetFields.add("non_public_metrics");
256+
tweetFields.add("promoted_metrics");
257+
tweetFields.add("organic_metrics");
258+
259+
// Get all available fields excluding Tweet Fields `non_public_metrics`, `promoted_metrics` & `organic_metrics`
260+
Get2TweetsIdResponse result = apiInstance.tweets().findTweetById("20")
261+
.tweetFields(tweetFields).excludeInputFields().execute();
262+
263+
// Get all the response fields
264+
Get2TweetsIdResponse result2 = apiInstance.tweets().findTweetById("20").excludeInputFields().execute();
265+
266+
```
267+
268+
242269
## Documentation for API Endpoints
243270

244271
All URIs are relative to *https://api.twitter.com*

sdk.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sdk.exclude.fields=tweetFields, userFields, expansions, mediaFields, placeFields, pollFields, searchCountFields, spaceFields, complianceJobFields
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2020 Twitter, Inc.
3+
SPDX-License-Identifier: Apache-2.0
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
18+
https://openapi-generator.tech
19+
Do not edit the class manually.
20+
*/
21+
22+
23+
package com.twitter.clientlib;
24+
25+
import java.io.FileInputStream;
26+
import java.util.Arrays;
27+
import java.util.Collections;
28+
import java.util.List;
29+
import java.util.Properties;
30+
31+
32+
public class SDKConfig {
33+
private static final Properties sdkProperties = new Properties();
34+
private static final List<String> allowlistedExcludeInputFields;
35+
36+
static {
37+
try {
38+
sdkProperties.load(new FileInputStream("sdk.properties"));
39+
} catch (Exception e) {
40+
e.printStackTrace();
41+
}
42+
String sdkExcludeInputFields = sdkProperties.getProperty("sdk.exclude.fields");
43+
sdkExcludeInputFields = sdkExcludeInputFields == null ? "" : sdkExcludeInputFields;
44+
allowlistedExcludeInputFields = Collections.unmodifiableList(Arrays.asList(sdkExcludeInputFields.split("[, ]+")));
45+
}
46+
47+
public static boolean isFieldAllowlisted(String field ) {
48+
return allowlistedExcludeInputFields.contains(field);
49+
}
50+
}

src/main/java/com/twitter/clientlib/api/ApiCommon.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222

2323
package com.twitter.clientlib.api;
2424

25-
import com.github.scribejava.core.model.OAuth2AccessToken;
26-
2725
import java.util.Calendar;
2826
import java.util.List;
27+
import java.util.Set;
28+
import java.util.stream.Collectors;
2929

3030
import com.twitter.clientlib.ApiClient;
3131
import com.twitter.clientlib.ApiException;
32+
import com.twitter.clientlib.SDKConfig;
3233

3334
public class ApiCommon {
3435
protected ApiClient localVarApiClient;
@@ -73,5 +74,13 @@ boolean isRateLimitRemaining(ApiException e) {
7374
return xRateLimitRemaining != null && xRateLimitRemaining.get(0) != null
7475
&& Long.parseLong(xRateLimitRemaining.get(0)) == 0;
7576
}
77+
78+
Set<String> getFields(String fieldName, boolean isExclude, Set<String> fieldValues, Set<String> allFieldsValues) {
79+
Set<String> result = fieldValues;
80+
if(isExclude && SDKConfig.isFieldAllowlisted(fieldName) && allFieldsValues != null) {
81+
result = allFieldsValues.stream().filter(e -> !(fieldValues != null && fieldValues.contains(e))).collect(Collectors.toSet());
82+
}
83+
return result;
84+
}
7685
}
7786

src/main/java/com/twitter/clientlib/api/BookmarksApi.java

+27
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.gson.reflect.TypeToken;
3737

3838
import java.io.IOException;
39+
import java.util.HashSet;
3940

4041

4142
import com.twitter.clientlib.model.BookmarkAddRequest;
@@ -167,6 +168,19 @@ public class APIgetUsersIdBookmarksRequest {
167168
private Set<String> pollFields;
168169
private Set<String> userFields;
169170
private Set<String> placeFields;
171+
private final Set<String> tweetFieldsAll = new HashSet<>(Arrays.asList("attachments", "author_id", "context_annotations", "conversation_id", "created_at", "entities", "geo", "id", "in_reply_to_user_id", "lang", "non_public_metrics", "organic_metrics", "possibly_sensitive", "promoted_metrics", "public_metrics", "referenced_tweets", "reply_settings", "source", "text", "withheld"));
172+
private final Set<String> expansionsAll = new HashSet<>(Arrays.asList("attachments.media_keys", "attachments.poll_ids", "author_id", "entities.mentions.username", "geo.place_id", "in_reply_to_user_id", "referenced_tweets.id", "referenced_tweets.id.author_id"));
173+
private final Set<String> mediaFieldsAll = new HashSet<>(Arrays.asList("alt_text", "duration_ms", "height", "media_key", "non_public_metrics", "organic_metrics", "preview_image_url", "promoted_metrics", "public_metrics", "type", "url", "variants", "width"));
174+
private final Set<String> pollFieldsAll = new HashSet<>(Arrays.asList("duration_minutes", "end_datetime", "id", "options", "voting_status"));
175+
private final Set<String> userFieldsAll = new HashSet<>(Arrays.asList("created_at", "description", "entities", "id", "location", "name", "pinned_tweet_id", "profile_image_url", "protected", "public_metrics", "url", "username", "verified", "withheld"));
176+
private final Set<String> placeFieldsAll = new HashSet<>(Arrays.asList("contained_within", "country", "country_code", "full_name", "geo", "id", "name", "place_type"));
177+
178+
private boolean isExclude = false;
179+
180+
public APIgetUsersIdBookmarksRequest excludeInputFields() {
181+
isExclude = true;
182+
return this;
183+
}
170184

171185
private APIgetUsersIdBookmarksRequest(String id) {
172186
this.id = id;
@@ -280,6 +294,13 @@ public okhttp3.Call buildCall(final ApiCallback _callback) throws ApiException {
280294
</table>
281295
*/
282296
public Get2UsersIdBookmarksResponse execute() throws ApiException {
297+
tweetFields = getFields("tweetFields", isExclude, tweetFields, tweetFieldsAll);
298+
expansions = getFields("expansions", isExclude, expansions, expansionsAll);
299+
mediaFields = getFields("mediaFields", isExclude, mediaFields, mediaFieldsAll);
300+
pollFields = getFields("pollFields", isExclude, pollFields, pollFieldsAll);
301+
userFields = getFields("userFields", isExclude, userFields, userFieldsAll);
302+
placeFields = getFields("placeFields", isExclude, placeFields, placeFieldsAll);
303+
283304
ApiResponse<Get2UsersIdBookmarksResponse> localVarResp = getUsersIdBookmarksWithHttpInfo(id, maxResults, paginationToken, tweetFields, expansions, mediaFields, pollFields, userFields, placeFields);
284305
return localVarResp.getData();
285306
}
@@ -422,6 +443,8 @@ private okhttp3.Call postUsersIdBookmarksAsync(BookmarkAddRequest bookmarkAddReq
422443
public class APIpostUsersIdBookmarksRequest {
423444
private final BookmarkAddRequest bookmarkAddRequest;
424445
private final String id;
446+
447+
425448

426449
private APIpostUsersIdBookmarksRequest(BookmarkAddRequest bookmarkAddRequest, String id) {
427450
this.bookmarkAddRequest = bookmarkAddRequest;
@@ -456,6 +479,7 @@ public okhttp3.Call buildCall(final ApiCallback _callback) throws ApiException {
456479
</table>
457480
*/
458481
public BookmarkMutationResponse execute() throws ApiException {
482+
459483
ApiResponse<BookmarkMutationResponse> localVarResp = postUsersIdBookmarksWithHttpInfo(bookmarkAddRequest, id);
460484
return localVarResp.getData();
461485
}
@@ -600,6 +624,8 @@ private okhttp3.Call usersIdBookmarksDeleteAsync(String id, String tweetId, fina
600624
public class APIusersIdBookmarksDeleteRequest {
601625
private final String id;
602626
private final String tweetId;
627+
628+
603629

604630
private APIusersIdBookmarksDeleteRequest(String id, String tweetId) {
605631
this.id = id;
@@ -634,6 +660,7 @@ public okhttp3.Call buildCall(final ApiCallback _callback) throws ApiException {
634660
</table>
635661
*/
636662
public BookmarkMutationResponse execute() throws ApiException {
663+
637664
ApiResponse<BookmarkMutationResponse> localVarResp = usersIdBookmarksDeleteWithHttpInfo(id, tweetId);
638665
return localVarResp.getData();
639666
}

src/main/java/com/twitter/clientlib/api/ComplianceApi.java

+24
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.gson.reflect.TypeToken;
3737

3838
import java.io.IOException;
39+
import java.util.HashSet;
3940

4041

4142
import com.twitter.clientlib.model.CreateComplianceJobRequest;
@@ -127,6 +128,8 @@ private okhttp3.Call createBatchComplianceJobAsync(CreateComplianceJobRequest cr
127128

128129
public class APIcreateBatchComplianceJobRequest {
129130
private final CreateComplianceJobRequest createComplianceJobRequest;
131+
132+
130133

131134
private APIcreateBatchComplianceJobRequest(CreateComplianceJobRequest createComplianceJobRequest) {
132135
this.createComplianceJobRequest = createComplianceJobRequest;
@@ -160,6 +163,7 @@ public okhttp3.Call buildCall(final ApiCallback _callback) throws ApiException {
160163
</table>
161164
*/
162165
public CreateComplianceJobResponse execute() throws ApiException {
166+
163167
ApiResponse<CreateComplianceJobResponse> localVarResp = createBatchComplianceJobWithHttpInfo(createComplianceJobRequest);
164168
return localVarResp.getData();
165169
}
@@ -301,6 +305,14 @@ private okhttp3.Call getBatchComplianceJobAsync(String id, Set<String> complianc
301305
public class APIgetBatchComplianceJobRequest {
302306
private final String id;
303307
private Set<String> complianceJobFields;
308+
private final Set<String> complianceJobFieldsAll = new HashSet<>(Arrays.asList("created_at", "download_expires_at", "download_url", "id", "name", "resumable", "status", "type", "upload_expires_at", "upload_url"));
309+
310+
private boolean isExclude = false;
311+
312+
public APIgetBatchComplianceJobRequest excludeInputFields() {
313+
isExclude = true;
314+
return this;
315+
}
304316

305317
private APIgetBatchComplianceJobRequest(String id) {
306318
this.id = id;
@@ -344,6 +356,8 @@ public okhttp3.Call buildCall(final ApiCallback _callback) throws ApiException {
344356
</table>
345357
*/
346358
public Get2ComplianceJobsIdResponse execute() throws ApiException {
359+
complianceJobFields = getFields("complianceJobFields", isExclude, complianceJobFields, complianceJobFieldsAll);
360+
347361
ApiResponse<Get2ComplianceJobsIdResponse> localVarResp = getBatchComplianceJobWithHttpInfo(id, complianceJobFields);
348362
return localVarResp.getData();
349363
}
@@ -493,6 +507,14 @@ public class APIlistBatchComplianceJobsRequest {
493507
private final String type;
494508
private String status;
495509
private Set<String> complianceJobFields;
510+
private final Set<String> complianceJobFieldsAll = new HashSet<>(Arrays.asList("created_at", "download_expires_at", "download_url", "id", "name", "resumable", "status", "type", "upload_expires_at", "upload_url"));
511+
512+
private boolean isExclude = false;
513+
514+
public APIlistBatchComplianceJobsRequest excludeInputFields() {
515+
isExclude = true;
516+
return this;
517+
}
496518

497519
private APIlistBatchComplianceJobsRequest(String type) {
498520
this.type = type;
@@ -546,6 +568,8 @@ public okhttp3.Call buildCall(final ApiCallback _callback) throws ApiException {
546568
</table>
547569
*/
548570
public Get2ComplianceJobsResponse execute() throws ApiException {
571+
complianceJobFields = getFields("complianceJobFields", isExclude, complianceJobFields, complianceJobFieldsAll);
572+
549573
ApiResponse<Get2ComplianceJobsResponse> localVarResp = listBatchComplianceJobsWithHttpInfo(type, status, complianceJobFields);
550574
return localVarResp.getData();
551575
}

src/main/java/com/twitter/clientlib/api/GeneralApi.java

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.gson.reflect.TypeToken;
3737

3838
import java.io.IOException;
39+
import java.util.HashSet;
3940

4041

4142

@@ -114,6 +115,8 @@ private okhttp3.Call getOpenApiSpecAsync(final ApiCallback<Object> _callback) th
114115
}
115116

116117
public class APIgetOpenApiSpecRequest {
118+
119+
117120

118121
private APIgetOpenApiSpecRequest() {
119122
}
@@ -144,6 +147,7 @@ public okhttp3.Call buildCall(final ApiCallback _callback) throws ApiException {
144147
</table>
145148
*/
146149
public Object execute() throws ApiException {
150+
147151
ApiResponse<Object> localVarResp = getOpenApiSpecWithHttpInfo();
148152
return localVarResp.getData();
149153
}

0 commit comments

Comments
 (0)