Skip to content

Commit e2f721b

Browse files
committed
Make spring-twitter-function as auto-config
* Fix Checkstyle violations in this module * Make all the Twitter function auto-configurations as conditional on their specific properties to avoid extra beans not expected in the target application * Fix README respectively
1 parent e1874ee commit e2f721b

14 files changed

+164
-175
lines changed

function/spring-twitter-function/README.adoc

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,31 @@
22

33
This module provides couple of twitter functions that can be reused and composed in other applications.
44

5-
To use those functions add the following dependency to your POM:
5+
This module exposes auto-configurations for the following beans:
66

7-
[source,XML]
8-
----
9-
<dependency>
10-
<groupId>org.springframework.cloud.fn</groupId>
11-
<artifactId>twitter-function</artifactId>
12-
<version>${java-functions.version}</version>
13-
</dependency>
14-
----
7+
* `twitterTrendFunction`
8+
* `twitterUsersFunction`
9+
* `twitterGeoFunction`
10+
11+
Each of them are conditional by specific configuration properties.
1512

1613
## 1. Twitter Trend Function
1714

18-
Functions can return either Trends topics or the Locations of the trending topics. The `twitter.trend.trend-query-type` property allows choosing between both types.
15+
Function can return either Trends topics or the Locations of the trending topics.
16+
The `twitter.trend.trend-query-type` property allows choosing between both types.
17+
This property is required to enable the function auto-configuration.
1918

20-
* Trends - `twitter.trend.trend-query-type` is set to `trend`. Leverages the https://developer.twitter.com/en/docs/trends/trends-for-location/api-reference/get-trends-place[Trends API] to return the https://help.twitter.com/en/using-twitter/twitter-trending-faqs[trending topics] near a specific latitude, longitude location.
19+
* Trends - `twitter.trend.trend-query-type` is set to `trend`.
20+
Leverages the https://developer.twitter.com/en/docs/trends/trends-for-location/api-reference/get-trends-place[Trends API] to return the https://help.twitter.com/en/using-twitter/twitter-trending-faqs[trending topics] near a specific latitude, longitude location.
2121
22-
* Trend Locations - the `twitter.trend.trend-query-type` is set `trendLocation`. Retrieve a full or nearby locations list of trending topics by location. If the `latitude`, `longitude` parameters are NOT provided the processor performs the https://developer.twitter.com/en/docs/trends/locations-with-trending-topics/api-reference/get-trends-available[Trends Available API] and returns the locations that Twitter has trending topic information for.
22+
* Trend Locations - the `twitter.trend.trend-query-type` is set `trendLocation`.
23+
Retrieve a full or nearby locations list of trending topics by location. If the `latitude`, `longitude` parameters are NOT provided the processor performs the https://developer.twitter.com/en/docs/trends/locations-with-trending-topics/api-reference/get-trends-available[Trends Available API] and returns the locations that Twitter has trending topic information for.
2324
If the `latitude`, `longitude` parameters are provided the processor performs the https://developer.twitter.com/en/docs/trends/locations-with-trending-topics/api-reference/get-trends-closest[Trends Closest API] and returns the locations that Twitter has trending topic information for, closest to a specified location.
2425
2526
Response is an array of `locations` that encode the location's WOEID and some other human-readable information such as a canonical name and country the location belongs in.
2627

2728
### 1.1 Beans for injection
2829

29-
You can import the `TwitterTrendFunctionConfiguration` in a Spring Boot application and then inject the following bean.
30-
31-
`filterFunction`
32-
3330
You can use `Function<Message<?>, Message<byte[]>> twitterTrendFunction` as a qualifier when injecting.
3431

3532
Once injected, you can use the `apply` method of the `Function` to invoke it and get the result.
@@ -45,12 +42,11 @@ See this link:src/test/java/org/springframework/cloud/fn/twitter/trend/TwitterTr
4542

4643
### 1.4 Other usage
4744

48-
Leveraging the Spring Cloud Function composability, you can compose the Trend function in your boot app like this:
45+
Leveraging the Spring Cloud Function "composability", you can compose the Trend function in your boot app like this:
4946

5047
[source,Java]
5148
----
5249
@SpringBootApplication
53-
@Import(TwitterTrendFunctionConfiguration.class )
5450
public class MyTwitterTrendBootApp {
5551
5652
public static void main(String[] args) {
@@ -79,11 +75,9 @@ Some parameters in this method are only required based on the existence of other
7975

8076
NOTE: Limits: 15 requests / 15-min window (user auth).
8177

82-
### 2.1 Beans for injection
83-
84-
You can import the `TwitterGeoFunctionConfiguration` in a Spring Boot application and then inject the following bean.
78+
This function auto-configuration is conditional on `twitter.geo.search.ip != null || (twitter.geo.location.lat != null && twitter.geo.location.lon != null)`
8579

86-
`filterFunction`
80+
### 2.1 Beans for injection
8781

8882
You can use `Function<Message<?>, Message<byte[]>> twitterGeoFunction` as a qualifier when injecting.
8983
Once injected, you can use the `apply` method of the `Function` to invoke it and get the result.
@@ -96,7 +90,7 @@ For more information on the various options available, please see link:../twitte
9690

9791
### 2.3 Tests
9892

99-
See this link:src/test/java/org/springframework/cloud/fn/twitter/geo/TwitterGeoFunctionTest.java[test suite] for examples of how this function is used.
93+
See this link:src/test/java/org/springframework/cloud/fn/twitter/geo/TwitterGeoFunctionTests.java[test suite] for examples of how this function is used.
10094

10195
### 2.4 Other usage
10296

@@ -105,7 +99,6 @@ Leveraging the Spring Cloud Function composability, you can compose the Geo func
10599
[source,Java]
106100
----
107101
@SpringBootApplication
108-
@Import(TwitterGeoFunctionConfiguration.class )
109102
public class MyTwitterGeoProcessorBootApp {
110103
111104
public static void main(String[] args) {
@@ -122,17 +115,14 @@ Uses SpEL expressions to compute the query parameters from the input message.
122115
Use the single quoted literals to set static values (e.g. user-id: '6666, 9999, 10000').
123116

124117
Use `twitter.users.type` property allow to select the query approaches.
118+
This property is required to trigger the `twitterUsersFunction` auto-configuration.
125119

126120
* https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-lookup[Users Lookup API] - Returns fully-hydrated user objects for up to 100 users per request, as specified by comma-separated values passed to the `userId` and/or `screenName` parameters. Rate limits: (300 requests / 15-min window)
127121
* https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-search[Users Search API] - Relevance-based search interface to public user accounts on Twitter.
128122
Querying by topical interest, full name, company name, location, or other criteria. Exact match searches are not supported. Only the first 1,000 matching results are available. Rate limits:(900 requests / 15-min window)
129123
130124
### 3.1 Beans for injection
131125

132-
You can import the `TwitterUsersFunctionConfiguration` in a Spring Boot application and then inject the following bean.
133-
134-
`filterFunction`
135-
136126
You can use `Function<Message<?>, Message<byte[]>> twitterUsersFunction` as a qualifier when injecting.
137127
Once injected, you can use the `apply` method of the `Function` to invoke it and get the result.
138128

function/spring-twitter-function/src/main/java/org/springframework/cloud/fn/twitter/geo/TwitterGeoFunctionConfiguration.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2020 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,27 +27,29 @@
2727
import twitter4j.Twitter;
2828
import twitter4j.TwitterException;
2929

30+
import org.springframework.boot.autoconfigure.AutoConfiguration;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
3032
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3133
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3234
import org.springframework.cloud.fn.common.twitter.TwitterConnectionConfiguration;
3335
import org.springframework.context.annotation.Bean;
34-
import org.springframework.context.annotation.Configuration;
35-
import org.springframework.context.annotation.Import;
3636
import org.springframework.messaging.Message;
3737

3838
/**
39+
* Auto-configuration for Twitter Geo function.
40+
*
3941
* @author Christian Tzolov
4042
*/
41-
@Configuration
43+
@ConditionalOnExpression("environment['twitter.geo.search.ip'] != null || (environment['twitter.geo.location.lat'] != null && environment['twitter.geo.location.lon'] != null)")
44+
@AutoConfiguration(after = TwitterConnectionConfiguration.class)
4245
@EnableConfigurationProperties(TwitterGeoFunctionProperties.class)
43-
@Import(TwitterConnectionConfiguration.class)
4446
public class TwitterGeoFunctionConfiguration {
4547

46-
private static final Log logger = LogFactory.getLog(TwitterGeoFunctionConfiguration.class);
48+
private static final Log LOGGER = LogFactory.getLog(TwitterGeoFunctionConfiguration.class);
4749

4850
@Bean
4951
public Function<Message<?>, GeoQuery> messageToGeoQueryFunction(TwitterGeoFunctionProperties geoProperties) {
50-
return message -> {
52+
return (message) -> {
5153
String ip = null;
5254
if (geoProperties.getSearch().getIp() != null) {
5355
ip = geoProperties.getSearch().getIp().getValue(message, String.class);
@@ -76,12 +78,12 @@ public Function<Message<?>, GeoQuery> messageToGeoQueryFunction(TwitterGeoFuncti
7678
@Bean
7779
@ConditionalOnProperty(name = "twitter.geo.search.type", havingValue = "search", matchIfMissing = true)
7880
public Function<GeoQuery, List<Place>> twitterSearchPlacesFunction(Twitter twitter) {
79-
return geoQuery -> {
81+
return (geoQuery) -> {
8082
try {
8183
return twitter.searchPlaces(geoQuery);
8284
}
83-
catch (TwitterException e) {
84-
logger.error("Places Search failed!", e);
85+
catch (TwitterException ex) {
86+
LOGGER.error("Places Search failed!", ex);
8587
}
8688
return null;
8789
};
@@ -90,12 +92,12 @@ public Function<GeoQuery, List<Place>> twitterSearchPlacesFunction(Twitter twitt
9092
@Bean
9193
@ConditionalOnProperty(name = "twitter.geo.search.type", havingValue = "reverse")
9294
public Function<GeoQuery, List<Place>> twitterReverseGeocodeFunction(Twitter twitter) {
93-
return geoQuery -> {
95+
return (geoQuery) -> {
9496
try {
9597
return twitter.reverseGeoCode(geoQuery);
9698
}
97-
catch (TwitterException e) {
98-
logger.error("Reverse Geocode failed!", e);
99+
catch (TwitterException ex) {
100+
LOGGER.error("Reverse Geocode failed!", ex);
99101
}
100102
return null;
101103
};
@@ -105,7 +107,7 @@ public Function<GeoQuery, List<Place>> twitterReverseGeocodeFunction(Twitter twi
105107
public Function<Message<?>, Message<byte[]>> twitterGeoFunction(Function<Message<?>, GeoQuery> toGeoQuery,
106108
Function<GeoQuery, List<Place>> places, Function<Object, Message<byte[]>> managedJson) {
107109

108-
return toGeoQuery.andThen(places).andThen(managedJson)::apply;
110+
return toGeoQuery.andThen(places).andThen(managedJson);
109111
}
110112

111113
}

function/spring-twitter-function/src/main/java/org/springframework/cloud/fn/twitter/geo/TwitterGeoFunctionProperties.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2020 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,18 +21,17 @@
2121

2222
import org.springframework.boot.context.properties.ConfigurationProperties;
2323
import org.springframework.expression.Expression;
24-
import org.springframework.expression.spel.standard.SpelExpressionParser;
2524
import org.springframework.validation.annotation.Validated;
2625

2726
/**
27+
* Configuration properties for Twitter Geo function.
28+
*
2829
* @author Christian Tzolov
2930
*/
3031
@ConfigurationProperties("twitter.geo")
3132
@Validated
3233
public class TwitterGeoFunctionProperties {
3334

34-
private static final Expression DEFAULT_EXPRESSION = new SpelExpressionParser().parseExpression("payload");
35-
3635
public enum GeoType {
3736

3837
/** Geo retrieval type. */
@@ -51,9 +50,6 @@ public enum GeoType {
5150
*/
5251
private Search search = new Search();
5352

54-
/**
55-
*
56-
*/
5753
private Location location = new Location();
5854

5955
/**
@@ -68,7 +64,7 @@ public enum GeoType {
6864
* in meters, but it can also take a string that is suffixed with ft to specify feet.
6965
* If this is not passed in, then it is assumed to be 0m. If coming from a device, in
7066
* practice, this value is whatever accuracy the device has measuring its location
71-
* (whether it be coming from a GPS, WiFi triangulation, etc.).
67+
* (whether it be coming from a GPS, Wi-Fi triangulation, etc.).
7268
*/
7369
private String accuracy = null;
7470

@@ -79,47 +75,47 @@ public enum GeoType {
7975
private String granularity = null;
8076

8177
public Search getSearch() {
82-
return search;
78+
return this.search;
8379
}
8480

8581
public void setSearch(Search search) {
8682
this.search = search;
8783
}
8884

8985
public GeoType getType() {
90-
return type;
86+
return this.type;
9187
}
9288

9389
public void setType(GeoType type) {
9490
this.type = type;
9591
}
9692

9793
public Location getLocation() {
98-
return location;
94+
return this.location;
9995
}
10096

10197
public void setLocation(Location location) {
10298
this.location = location;
10399
}
104100

105101
public int getMaxResults() {
106-
return maxResults;
102+
return this.maxResults;
107103
}
108104

109105
public void setMaxResults(int maxResults) {
110106
this.maxResults = maxResults;
111107
}
112108

113109
public String getAccuracy() {
114-
return accuracy;
110+
return this.accuracy;
115111
}
116112

117113
public void setAccuracy(String accuracy) {
118114
this.accuracy = accuracy;
119115
}
120116

121117
public String getGranularity() {
122-
return granularity;
118+
return this.granularity;
123119
}
124120

125121
public void setGranularity(String granularity) {
@@ -128,13 +124,12 @@ public void setGranularity(String granularity) {
128124

129125
@AssertTrue(message = "Either the IP or the Location must be set")
130126
public boolean isAtLeastOne() {
131-
return this.getSearch().getIp() == null
132-
^ (this.getLocation().getLat() == null && this.getLocation().getLon() == null);
127+
return getSearch().getIp() == null ^ (getLocation().getLat() == null && getLocation().getLon() == null);
133128
}
134129

135130
@AssertTrue(message = "The IP parameter is applicable only for 'Search' GeoType")
136131
public boolean isIpUsedWithSearchGeoType() {
137-
if (this.getSearch().getIp() != null) {
132+
if (getSearch().getIp() != null) {
138133
return this.type == GeoType.search;
139134
}
140135
return true;
@@ -154,15 +149,15 @@ public static class Search {
154149
private Expression query = null;
155150

156151
public Expression getIp() {
157-
return ip;
152+
return this.ip;
158153
}
159154

160155
public void setIp(Expression ip) {
161156
this.ip = ip;
162157
}
163158

164159
public Expression getQuery() {
165-
return query;
160+
return this.query;
166161
}
167162

168163
public void setQuery(Expression query) {
@@ -184,15 +179,15 @@ public static class Location {
184179
private Expression lon;
185180

186181
public Expression getLat() {
187-
return lat;
182+
return this.lat;
188183
}
189184

190185
public void setLat(Expression lat) {
191186
this.lat = lat;
192187
}
193188

194189
public Expression getLon() {
195-
return lon;
190+
return this.lon;
196191
}
197192

198193
public void setLon(Expression lon) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* The Twitter Geo function auto-configuration.
3+
*/
4+
package org.springframework.cloud.fn.twitter.geo;

0 commit comments

Comments
 (0)