Package ch.bztf.m226blb1.Base
Class BaseService
java.lang.Object
ch.bztf.m226blb1.Base.BaseService
- Direct Known Subclasses:
InstagramService
,TwitterService
This class is the base class for all other services. It contains all the
repositories and the
web client
.- Category:
- Base
- API Note:
- Responses are of type
ResponseHandler
- Implementation Note:
- This class is not intended to be used directly.
The inherited classes need to have the annotation
@Service
-
Field Summary
Modifier and TypeFieldDescriptionprotected AccountRepository
AccountRepository
for the accounts with the authenticationprotected PostRepository
PostRepository
for saving the postsprotected UserRepository
UserRepository
for caching the usersprotected final org.springframework.web.reactive.function.client.WebClient
WebClient for requesting data from the API -
Constructor Summary
ModifierConstructorDescriptionprotected
BaseService
(Platform platform) Constructor which sets up all valuesprotected
BaseService
(Platform platform, @NonNull org.springframework.web.reactive.function.client.WebClient webClient) Constructor which sets up all values -
Method Summary
Modifier and TypeMethodDescriptionstatic org.springframework.web.reactive.function.client.WebClient.Builder
Returns thebuilder
for building theweb client
getJsonData
(@NonNull String requestUri, @NotBlank @Size(min=128,max=128) String apiKey, @NotBlank String username) Gets the user json data from the APIprotected abstract String
getPostRequestUri
(String username, int limit) Gets the uri for requesting the posts datagetPosts
(@NotBlank @Size(min=128,max=128) String apiKey, @NotBlank String username, @javax.validation.constraints.NotBlank,@javax.validation.constraints.PositiveOrZero int limit) protected abstract String
getUserRequestUri
(String username) Gets the uri for requesting the user datamapJsonToPosts
(@NotBlank String json, @javax.validation.constraints.PositiveOrZero int limit) Maps the JSON data to a set ofposts
abstract User
mapJsonToUser
(@NotBlank String json) Maps the JSON data to auser
object
-
Field Details
-
webClient
protected final org.springframework.web.reactive.function.client.WebClient webClientWebClient for requesting data from the API- See Also:
-
WebClient
-
accountRepository
AccountRepository
for the accounts with the authentication -
userRepository
UserRepository
for caching the users -
postRepository
PostRepository
for saving the posts
-
-
Constructor Details
-
BaseService
Constructor which sets up all values- Parameters:
platform
- Theplatform
, for which the service is responsible
-
BaseService
protected BaseService(Platform platform, @NonNull @NonNull org.springframework.web.reactive.function.client.WebClient webClient) Constructor which sets up all values- Parameters:
platform
- Theplatform
, for which the service is responsiblewebClient
- Theweb client
for requesting data
-
-
Method Details
-
getBaseWebClient
public static org.springframework.web.reactive.function.client.WebClient.Builder getBaseWebClient()Returns thebuilder
for building theweb client
- Returns:
- The web client builder
-
getUser
public ResponseHandler<User> getUser(@NotBlank @Size(min=128,max=128) @NotBlank @Size(min=128,max=128) String apiKey, @NotBlank @NotBlank String username) - Parameters:
apiKey
-API key
of theAccount
which makes the requestusername
- Username of the user- Returns:
ResponseHandler
with the userusername
or an error
-
getPosts
public ResponseHandler<Set<Post>> getPosts(@NotBlank @Size(min=128,max=128) @NotBlank @Size(min=128,max=128) String apiKey, @NotBlank @NotBlank String username, @NotBlank @PositiveOrZero @javax.validation.constraints.NotBlank,@javax.validation.constraints.PositiveOrZero int limit) - Parameters:
apiKey
-API key
of theAccount
which makes the requestusername
- Username of the userlimit
- Limit of the posts- Returns:
ResponseHandler
with the lastlimit
posts fromusername
or an error
-
getJsonData
public ResponseHandler<String> getJsonData(@NonNull @NonNull String requestUri, @NotBlank @Size(min=128,max=128) @NotBlank @Size(min=128,max=128) String apiKey, @NotBlank @NotBlank String username) Gets the user json data from the API- Parameters:
requestUri
- URI of the requestapiKey
-API key
of theAccount
which makes the requestusername
- username of the Instagram user- Returns:
ResponseHandler
with the json data or an error message- See Also:
-
webClient
handleResponse(long, ClientResponse, Platform)
getUserRequestUri(String)
getPostRequestUri(String, int)
- Implementation Note:
- This method uses the rate limiter to limit the requests.
-
mapJsonToUser
Maps the JSON data to auser
object- Parameters:
json
- the JSON data to map- Returns:
- the created
user
object - See Also:
-
ObjectMapper
-
mapJsonToPosts
public abstract Set<Post> mapJsonToPosts(@NotBlank @NotBlank String json, @PositiveOrZero @javax.validation.constraints.PositiveOrZero int limit) Maps the JSON data to a set ofposts
- Parameters:
json
- the JSON data to maplimit
- the limit of theposts
- Returns:
- the created set of posts limited by
limit
- See Also:
-
ObjectMapper
-
getUserRequestUri
Gets the uri for requesting the user data- Parameters:
username
- the username of the user- Returns:
- the uri for requesting the user data
- See Also:
-
User
-
getPostRequestUri
Gets the uri for requesting the posts data- Parameters:
username
- the username of the userlimit
- the limit of the posts- Returns:
- the uri for requesting the posts data
- See Also:
-
User
-