From c25b79658a39a4488044428fff0017be969691ab Mon Sep 17 00:00:00 2001 From: Tomasz Rosiek Date: Tue, 14 May 2024 09:42:57 +0200 Subject: [PATCH 1/2] WIP: create likes firehose --- pom.xml | 2 +- .../com/twitter/clientlib/api/LikesApi.java | 339 ++++++++++++++++++ .../com/twitter/clientlib/api/TwitterApi.java | 5 + .../twitter/clientlib/api/LikesApiTest.java | 73 ++++ 4 files changed, 418 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/twitter/clientlib/api/LikesApi.java create mode 100644 src/test/java/com/twitter/clientlib/api/LikesApiTest.java diff --git a/pom.xml b/pom.xml index 348f7f3..a280047 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ twitter-api-java-sdk jar twitter-api-java-sdk - 2.0.3 + 2.0.123 https://github.com/twitterdev/twitter-api-java-sdk Twitter API v2 available endpoints diff --git a/src/main/java/com/twitter/clientlib/api/LikesApi.java b/src/main/java/com/twitter/clientlib/api/LikesApi.java new file mode 100644 index 0000000..626c0f2 --- /dev/null +++ b/src/main/java/com/twitter/clientlib/api/LikesApi.java @@ -0,0 +1,339 @@ +/* +Copyright 2020 Twitter, Inc. +SPDX-License-Identifier: Apache-2.0 + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +https://openapi-generator.tech +Do not edit the class manually. +*/ + + +package com.twitter.clientlib.api; + +import com.google.gson.reflect.TypeToken; +import com.twitter.clientlib.ApiCallback; +import com.twitter.clientlib.ApiException; +import com.twitter.clientlib.ApiResponse; +import com.twitter.clientlib.Pair; +import com.twitter.clientlib.model.*; + +import java.io.InputStream; +import java.lang.reflect.Type; +import java.time.OffsetDateTime; +import java.util.*; +// TODO: Adjust +public class LikesApi extends ApiCommon { + + + private okhttp3.Call getLikesFirehoseStreamCall(Integer backfillMinutes, Integer partition, OffsetDateTime startTime, OffsetDateTime endTime, Set likeFields ,Set tweetFields, Set expansions, Set userFields, final ApiCallback _callback) throws ApiException { + Object localVarPostBody = null; + + // create path and map variables + String localVarPath = "/2/likes/firehose/stream"; + + List localVarQueryParams = new ArrayList(); + List localVarCollectionQueryParams = new ArrayList(); + Map localVarHeaderParams = new HashMap(); + Map localVarCookieParams = new HashMap(); + Map localVarFormParams = new HashMap(); + + if (backfillMinutes != null) { + localVarQueryParams.addAll(localVarApiClient.parameterToPair("backfill_minutes", backfillMinutes)); + } + + if (partition != null) { + localVarQueryParams.addAll(localVarApiClient.parameterToPair("partition", partition)); + } + + if (startTime != null) { + localVarQueryParams.addAll(localVarApiClient.parameterToPair("start_time", startTime)); + } + + if (endTime != null) { + localVarQueryParams.addAll(localVarApiClient.parameterToPair("end_time", endTime)); + } + + if (tweetFields != null) { + localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("csv", "tweet.fields", tweetFields)); + } + + if (likeFields != null) { + localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("csv", "like.fields", likeFields)); + } + + if (expansions != null) { + localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("csv", "expansions", expansions)); + } + + if (userFields != null) { + localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("csv", "user.fields", userFields)); + } + + final String[] localVarAccepts = { + "application/json", "application/problem+json" + }; + final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts); + if (localVarAccept != null) { + localVarHeaderParams.put("Accept", localVarAccept); + } + + final String[] localVarContentTypes = { + + }; + final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes); + if (localVarContentType != null) { + localVarHeaderParams.put("Content-Type", localVarContentType); + } + + String[] localVarAuthNames = new String[]{"BearerToken"}; + return localVarApiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, reduceAuthNames(localVarAuthNames), _callback); + } + + @SuppressWarnings("rawtypes") + private okhttp3.Call getLikesFirehoseStreamValidateBeforeCall(Integer backfillMinutes, Integer partition, OffsetDateTime startTime, OffsetDateTime endTime, Set likeFields, Set tweetFields, Set expansions, Set userFields, final ApiCallback _callback) throws ApiException { + + // verify the required parameter 'partition' is set + if (partition == null) { + throw new ApiException("Missing the required parameter 'partition' when calling getLikesFirehoseStream(Async)"); + } + + + okhttp3.Call localVarCall = getLikesFirehoseStreamCall(backfillMinutes, partition, startTime, endTime, likeFields, tweetFields, expansions, userFields, _callback); + return localVarCall; + + } + + + private InputStream getLikesFirehoseStreamWithHttpInfo(Integer backfillMinutes, Integer partition, OffsetDateTime startTime, OffsetDateTime endTime, Set likeFields, Set tweetFields, Set expansions, Set userFields) throws ApiException { + okhttp3.Call localVarCall = getLikesFirehoseStreamValidateBeforeCall(backfillMinutes, partition, startTime, endTime, likeFields, tweetFields, expansions, userFields, null); + try { + Type localVarReturnType = new TypeToken() { + }.getType(); + return localVarApiClient.executeStream(localVarCall, localVarReturnType); + } catch (ApiException e) { + e.setErrorObject(localVarApiClient.getJSON().getGson().fromJson(e.getResponseBody(), new TypeToken() { + }.getType())); + throw e; + } + } + + private okhttp3.Call getLikesFirehoseStreamAsync(Integer backfillMinutes, Integer partition, OffsetDateTime startTime, OffsetDateTime endTime, Set likeFields, Set tweetFields, Set expansions, Set userFields, final ApiCallback _callback) throws ApiException { + + okhttp3.Call localVarCall = getLikesFirehoseStreamValidateBeforeCall(backfillMinutes, partition, startTime, endTime, likeFields, tweetFields, expansions, userFields, _callback); + Type localVarReturnType = new TypeToken() { + }.getType(); + localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback); + return localVarCall; + } + + public class APIgetLikesFirehoseStreamRequest { + private final Integer partition; + private Integer backfillMinutes; + private OffsetDateTime startTime; + private OffsetDateTime endTime; + private Set likeFields; + private Set tweetFields; + private Set expansions; + private Set userFields; + private final Set likeFieldsAll = new HashSet<>(Arrays.asList("created_at", "id", "liked_tweet_id", "liking_user_id", "timestamp_ms")); + private final Set tweetFieldsAll = new HashSet<>(Arrays.asList("attachments", "author_id", "card_uri", "context_annotations", "conversation_id", "created_at", "edit_controls", "edit_history_tweet_ids", "entities", "geo", "id", "in_reply_to_user_id", "lang", "non_public_metrics", "note_tweet", "organic_metrics", "possibly_sensitive", "promoted_metrics", "public_metrics", "referenced_tweets", "reply_settings", "source", "text", "withheld")); + private final Set expansionsAll = new HashSet<>(Arrays.asList("liked_tweet_id", "liking_user_id")); + private final Set userFieldsAll = new HashSet<>(Arrays.asList("connection_status", "created_at", "description", "entities", "id", "location", "most_recent_tweet_id", "name", "pinned_tweet_id", "profile_image_url", "protected", "public_metrics", "receives_your_dm", "subscription_type", "url", "username", "verified", "verified_type", "withheld")); + + private boolean isExclude = false; + + public APIgetLikesFirehoseStreamRequest excludeInputFields() { + isExclude = true; + return this; + } + + private APIgetLikesFirehoseStreamRequest(Integer partition) { + this.partition = partition; + } + + /** + * Set backfillMinutes + * + * @param backfillMinutes The number of minutes of backfill requested. (optional) + * @return APIgetLikesFirehoseStreamRequest + */ + public APIgetLikesFirehoseStreamRequest backfillMinutes(Integer backfillMinutes) { + this.backfillMinutes = backfillMinutes; + return this; + } + + /** + * Set startTime + * + * @param startTime YYYY-MM-DDTHH:mm:ssZ. The earliest UTC timestamp to which the Tweets will be provided. (optional) + * @return APIgetLikesFirehoseStreamRequest + */ + public APIgetLikesFirehoseStreamRequest startTime(OffsetDateTime startTime) { + this.startTime = startTime; + return this; + } + + /** + * Set endTime + * + * @param endTime YYYY-MM-DDTHH:mm:ssZ. The latest UTC timestamp to which the Tweets will be provided. (optional) + * @return APIgetLikesFirehoseStreamRequest + */ + public APIgetLikesFirehoseStreamRequest endTime(OffsetDateTime endTime) { + this.endTime = endTime; + return this; + } + + /** + * Set likeFields + * + * @param likeFields A comma separated list of Tweet fields to display. (optional) + * @return APIgetLikesFirehoseStreamRequest + */ + public APIgetLikesFirehoseStreamRequest likeFields(Set likeFields) { + this.likeFields = likeFields; + return this; + } + + /** + * Set tweetFields + * + * @param tweetFields A comma separated list of Tweet fields to display. (optional) + * @return APIgetLikesFirehoseStreamRequest + */ + public APIgetLikesFirehoseStreamRequest tweetFields(Set tweetFields) { + this.tweetFields = tweetFields; + return this; + } + + /** + * Set expansions + * + * @param expansions A comma separated list of fields to expand. (optional) + * @return APIgetLikesFirehoseStreamRequest + */ + public APIgetLikesFirehoseStreamRequest expansions(Set expansions) { + this.expansions = expansions; + return this; + } + + /** + * Set userFields + * + * @param userFields A comma separated list of User fields to display. (optional) + * @return APIgetLikesFirehoseStreamRequest + */ + public APIgetLikesFirehoseStreamRequest userFields(Set userFields) { + this.userFields = userFields; + return this; + } + + /** + * Build call for getLikesFirehoseStream + * + * @param _callback ApiCallback API callback + * @return Call to execute + * @throws ApiException If fail to serialize the request body object + * @http.response.details + * + * + * + *
Status Code Description Response Headers
200 The request has succeeded. -
0 The request has failed. -
+ */ + public okhttp3.Call buildCall(final ApiCallback _callback) throws ApiException { + return getLikesFirehoseStreamCall(backfillMinutes, partition, startTime, endTime, likeFields, tweetFields, expansions, userFields, _callback); + } + + /** + * Execute getLikesFirehoseStream request + * + * @return StreamingTweetResponse + * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body + * @http.response.details + * + * + * + *
Status Code Description Response Headers
200 The request has succeeded. -
0 The request has failed. -
+ */ + public InputStream execute() throws ApiException { + return getLikesFirehoseStreamWithHttpInfo(backfillMinutes, partition, startTime, endTime, likeFields, tweetFields, expansions, userFields); + } + + /** + * Calls the API using a retry mechanism to handle rate limits errors. + */ + public InputStream execute(Integer retries) throws ApiException { + InputStream localVarResp; + try { + localVarResp = execute(); + } catch (ApiException e) { + if (handleRateLimit(e, retries)) { + return execute(retries - 1); + } else { + throw e; + } + } + return localVarResp; + } + + /** + * Execute getLikesFirehoseStream request with HTTP info returned + * + * @return ApiResponse<StreamingTweetResponse> + * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body + * @http.response.details + * + * + * + *
Status Code Description Response Headers
200 The request has succeeded. -
0 The request has failed. -
+ */ + + public InputStream executeWithHttpInfo() throws ApiException { + return getLikesFirehoseStreamWithHttpInfo(backfillMinutes, partition, startTime, endTime, likeFields, tweetFields, expansions, userFields); + } + + /** + * Execute getLikesFirehoseStream request (asynchronously) + * + * @param _callback The callback to be executed when the API call finishes + * @return The request call + * @throws ApiException If fail to process the API call, e.g. serializing the request body object + * @http.response.details + * + * + * + *
Status Code Description Response Headers
200 The request has succeeded. -
0 The request has failed. -
+ */ + public okhttp3.Call executeAsync(final ApiCallback _callback) throws ApiException { + return getLikesFirehoseStreamAsync(backfillMinutes, partition, startTime, endTime, likeFields, tweetFields, expansions, userFields, _callback); + } + } + + /** + * Firehose stream + * Streams 100% of public Likes. + * + * @param partition The partition number. (required) + * @return APIgetLikesFirehoseStreamRequest + * @http.response.details + * + * + * + *
Status Code Description Response Headers
200 The request has succeeded. -
0 The request has failed. -
+ */ + public APIgetLikesFirehoseStreamRequest getLikesFirehoseStream(Integer partition) { + return new APIgetLikesFirehoseStreamRequest(partition); + } +} diff --git a/src/main/java/com/twitter/clientlib/api/TwitterApi.java b/src/main/java/com/twitter/clientlib/api/TwitterApi.java index a20b38f..31d5085 100644 --- a/src/main/java/com/twitter/clientlib/api/TwitterApi.java +++ b/src/main/java/com/twitter/clientlib/api/TwitterApi.java @@ -41,6 +41,7 @@ public class TwitterApi { private final ListsApi lists = new ListsApi(); private final SpacesApi spaces = new SpacesApi(); private final TweetsApi tweets = new TweetsApi(); + private final LikesApi likes = new LikesApi(); private final UsersApi users = new UsersApi(); private ApiClient localVarApiClient = new ApiClient(); @@ -83,6 +84,7 @@ private void initApis() { lists.setClient(localVarApiClient); spaces.setClient(localVarApiClient); tweets.setClient(localVarApiClient); + likes.setClient(localVarApiClient); users.setClient(localVarApiClient); } @@ -107,6 +109,9 @@ public TweetsApi tweets() { public UsersApi users() { return users; } + public LikesApi likes() { + return likes; + } public OAuth2AccessToken refreshToken() throws ApiException { return localVarApiClient.refreshToken(); diff --git a/src/test/java/com/twitter/clientlib/api/LikesApiTest.java b/src/test/java/com/twitter/clientlib/api/LikesApiTest.java new file mode 100644 index 0000000..1cd7767 --- /dev/null +++ b/src/test/java/com/twitter/clientlib/api/LikesApiTest.java @@ -0,0 +1,73 @@ +/* +Copyright 2020 Twitter, Inc. +SPDX-License-Identifier: Apache-2.0 + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +https://openapi-generator.tech +Do not edit the class manually. +*/ + + +package com.twitter.clientlib.api; + +import com.twitter.clientlib.ApiException; +import com.twitter.clientlib.TwitterCredentialsBearer; +import com.twitter.clientlib.model.*; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import java.io.InputStream; +import java.time.OffsetDateTime; +import java.util.List; +import java.util.Set; + +/** + * API tests for LikesApi + */ +@Disabled +public class LikesApiTest { + + private final TwitterApi apiInstance = new TwitterApi(new TwitterCredentialsBearer(System.getenv("TWITTER_BEARER_TOKEN"))); + // TODO set credentials + + /** + * Firehose stream + * + * Streams 100% of public Likes. + * + * @throws ApiException if the Api call fails + */ + @Test + public void getTweetsFirehoseStreamTest() throws ApiException { + Integer backfillMinutes = null; + Integer partition = null; + OffsetDateTime startTime = null; + OffsetDateTime endTime = null; + Set likeFields = null; + Set tweetFields = null; + Set expansions = null; + Set userFields = null; + InputStream response = apiInstance.likes().getLikesFirehoseStream(partition) + .backfillMinutes(backfillMinutes) + .startTime(startTime) + .endTime(endTime) + .tweetFields(tweetFields) + .likeFields(likeFields) + .expansions(expansions) + .userFields(userFields) + .execute(); + // TODO: test validations + } +} From f0089be77311a39d61e5a745cffb1e15529f68ac Mon Sep 17 00:00:00 2001 From: Tomasz Rosiek Date: Mon, 27 May 2024 10:37:22 +0200 Subject: [PATCH 2/2] cleanup --- pom.xml | 2 +- src/main/java/com/twitter/clientlib/api/LikesApi.java | 1 - src/test/java/com/twitter/clientlib/api/LikesApiTest.java | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index a280047..711c4d5 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ twitter-api-java-sdk jar twitter-api-java-sdk - 2.0.123 + 2.1.0 https://github.com/twitterdev/twitter-api-java-sdk Twitter API v2 available endpoints diff --git a/src/main/java/com/twitter/clientlib/api/LikesApi.java b/src/main/java/com/twitter/clientlib/api/LikesApi.java index 626c0f2..b71c166 100644 --- a/src/main/java/com/twitter/clientlib/api/LikesApi.java +++ b/src/main/java/com/twitter/clientlib/api/LikesApi.java @@ -33,7 +33,6 @@ import java.lang.reflect.Type; import java.time.OffsetDateTime; import java.util.*; -// TODO: Adjust public class LikesApi extends ApiCommon { diff --git a/src/test/java/com/twitter/clientlib/api/LikesApiTest.java b/src/test/java/com/twitter/clientlib/api/LikesApiTest.java index 1cd7767..0530caf 100644 --- a/src/test/java/com/twitter/clientlib/api/LikesApiTest.java +++ b/src/test/java/com/twitter/clientlib/api/LikesApiTest.java @@ -40,7 +40,6 @@ public class LikesApiTest { private final TwitterApi apiInstance = new TwitterApi(new TwitterCredentialsBearer(System.getenv("TWITTER_BEARER_TOKEN"))); - // TODO set credentials /** * Firehose stream