Skip to content

Commit

Permalink
refactor: set default kraken headers (accept, content-type) using int…
Browse files Browse the repository at this point in the history
…erceptor (#257)

* refactor: set default kraken headers (accept, content-type) using interceptor
* chore: bump t4j user agent to twitch4j/1.2.0
* refactor: move json content-type interceptor into common module
  • Loading branch information
PhilippHeuer committed Jan 20, 2021
1 parent eeefaee commit d68e404
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class Twitch4JGlobal {
/**
* Default UserAgent
*/
public static String userAgent = "Twitch4J/1.0.0";
public static String userAgent = "Twitch4J/1.2.0";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.twitch4j.common.feign.interceptor;

import feign.Request;
import feign.RequestInterceptor;
import feign.RequestTemplate;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Injects the content-type application/json into all PUT,PATCH,POST requests that didn't specify a content-type
*/
public class JsonContentTypeHeaderInterceptor implements RequestInterceptor {

/**
* Holds all http methods that transmit a body (to set the content-type)
*/
private static final Set<String> methodsWithBody = Stream.of(Request.HttpMethod.POST.name(), Request.HttpMethod.PATCH.name(), Request.HttpMethod.PUT.name()).collect(Collectors.toSet());

/**
* Interceptor
*
* @param t Feign RequestTemplate
*/
@Override
public void apply(RequestTemplate t) {
// set content-type if not specified in the TwitchKraken interface for POST, PATCH, PUT requests
if (!t.headers().containsKey("Content-Type") && !t.headers().containsKey("content-type") && methodsWithBody.contains(t.method())) {
t.header("Content-Type", "application/json");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public interface TwitchKraken {
@RequestLine("GET /channels/{channelId}/editors")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
})
HystrixCommand<KrakenUserList> getChannelEditors(
@Param("token") String authToken,
Expand All @@ -65,9 +64,6 @@ HystrixCommand<KrakenUserList> getChannelEditors(
* @return {@link KrakenFollowList}
*/
@RequestLine("GET /channels/{channelId}/follows?limit={limit}&offset={offset}&cursor={cursor}&direction={direction}")
@Headers({
"Accept: application/vnd.twitchtv.v5+json"
})
HystrixCommand<KrakenFollowList> getChannelFollowers(
@Param("channelId") String channelId,
@Param("limit") Integer limit,
Expand All @@ -87,8 +83,7 @@ HystrixCommand<KrakenFollowList> getChannelFollowers(
*/
@RequestLine("DELETE /channels/{channelId}/stream_key")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
"Authorization: OAuth {token}"
})
HystrixCommand<KrakenChannel> resetChannelStreamKey(
@Param("token") String authToken,
Expand All @@ -109,8 +104,7 @@ HystrixCommand<KrakenChannel> resetChannelStreamKey(
*/
@RequestLine("GET /channels/{channelId}/subscriptions?limit={limit}&offset={offset}&direction={direction}")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
"Authorization: OAuth {token}"
})
HystrixCommand<KrakenSubscriptionList> getChannelSubscribers(
@Param("token") String authToken,
Expand All @@ -132,9 +126,7 @@ HystrixCommand<KrakenSubscriptionList> getChannelSubscribers(
@Unofficial
@RequestLine("POST /chat/twitchbot/approve")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json",
"Content-Type: application/json"
"Authorization: OAuth {token}"
})
@Body("%7B\"msg_id\":\"{msg_id}\"%7D")
HystrixCommand<Void> approveAutomodMessage(
Expand All @@ -154,9 +146,7 @@ HystrixCommand<Void> approveAutomodMessage(
@Unofficial
@RequestLine("POST /chat/twitchbot/deny")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json",
"Content-Type: application/json"
"Authorization: OAuth {token}"
})
@Body("%7B\"msg_id\":\"{msg_id}\"%7D")
HystrixCommand<Void> denyAutomodMessage(
Expand All @@ -173,9 +163,6 @@ HystrixCommand<Void> denyAutomodMessage(
* @return {@link KrakenClip}
*/
@RequestLine("GET /clips/{slug}")
@Headers({
"Accept: application/vnd.twitchtv.v5+json"
})
HystrixCommand<KrakenClip> getClip(
@Param("slug") String slug
);
Expand All @@ -189,9 +176,6 @@ HystrixCommand<KrakenClip> getClip(
* @return {@link KrakenCollectionMetadata}
*/
@RequestLine("GET /collections/{collection_id}")
@Headers({
"Accept: application/vnd.twitchtv.v5+json"
})
HystrixCommand<KrakenCollectionMetadata> getCollectionMetadata(
@Param("collection_id") String collectionId
);
Expand All @@ -205,9 +189,6 @@ HystrixCommand<KrakenCollectionMetadata> getCollectionMetadata(
* @return {@link KrakenCollection}
*/
@RequestLine("GET /collections/{collection_id}/items")
@Headers({
"Accept: application/vnd.twitchtv.v5+json"
})
HystrixCommand<KrakenCollection> getCollection(
@Param("collection_id") String collectionId
);
Expand All @@ -224,9 +205,6 @@ HystrixCommand<KrakenCollection> getCollection(
* @return {@link KrakenCollectionList}
*/
@RequestLine("GET /channels/{channel_id}/collections?limit={limit}&cursor={cursor}&containing_item={containing_item}")
@Headers({
"Accept: application/vnd.twitchtv.v5+json"
})
HystrixCommand<KrakenCollectionList> getCollectionsByChannel(
@Param("channel_id") String channelId,
@Param("limit") Integer limit,
Expand All @@ -246,9 +224,7 @@ HystrixCommand<KrakenCollectionList> getCollectionsByChannel(
*/
@RequestLine("POST /channels/{channel_id}/collections")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json",
"Content-Type: application/json"
"Authorization: OAuth {token}"
})
@Body("%7B\"title\":\"{title}\"%7D")
HystrixCommand<KrakenCollectionMetadata> createCollection(
Expand All @@ -269,9 +245,7 @@ HystrixCommand<KrakenCollectionMetadata> createCollection(
*/
@RequestLine("PUT /collections/{collection_id}")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json",
"Content-Type: application/json"
"Authorization: OAuth {token}"
})
@Body("%7B\"title\":\"{title}\"%7D")
HystrixCommand<Void> updateCollection(
Expand All @@ -293,8 +267,6 @@ HystrixCommand<Void> updateCollection(
@RequestLine("PUT /collections/{collection_id}/thumbnail")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json",
"Content-Type: application/json"
})
@Body("%7B\"item_id\":\"{item_id}\"%7D")
HystrixCommand<Void> createCollectionThumbnail(
Expand All @@ -314,8 +286,7 @@ HystrixCommand<Void> createCollectionThumbnail(
*/
@RequestLine("DELETE /collections/{collection_id}")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
"Authorization: OAuth {token}"
})
HystrixCommand<Void> deleteCollection(
@Param("token") String token,
Expand All @@ -335,8 +306,6 @@ HystrixCommand<Void> deleteCollection(
@RequestLine("POST /collections/{collection_id}/items")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json",
"Content-Type: application/json"
})
@Body("%7B\"id\":\"{id}\",\"type\":\"video\"%7D")
HystrixCommand<KrakenCollectionItem> addItemToCollection(
Expand All @@ -357,8 +326,7 @@ HystrixCommand<KrakenCollectionItem> addItemToCollection(
*/
@RequestLine("DELETE /collections/{collection_id}/items/{collection_item_id}")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
"Authorization: OAuth {token}"
})
HystrixCommand<Void> deleteItemFromCollection(
@Param("token") String token,
Expand All @@ -379,9 +347,7 @@ HystrixCommand<Void> deleteItemFromCollection(
*/
@RequestLine("PUT /collections/{collection_id}/items/{collection_item_id}")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json",
"Content-Type: application/json"
"Authorization: OAuth {token}"
})
@Body("%7B\"position\":\"{position}\"%7D")
HystrixCommand<Void> moveItemWithinCollection(
Expand All @@ -404,8 +370,7 @@ HystrixCommand<Void> moveItemWithinCollection(
*/
@RequestLine("GET /users/{user}/blocks?limit={limit}&offset={offset}")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
"Authorization: OAuth {token}"
})
HystrixCommand<KrakenBlockList> getUserBlockList(
@Param("token") String authToken,
Expand All @@ -426,8 +391,7 @@ HystrixCommand<KrakenBlockList> getUserBlockList(
*/
@RequestLine("PUT /users/{from_id}/blocks/{to_id}")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
"Authorization: OAuth {token}"
})
HystrixCommand<KrakenBlockTransaction> blockUser(
@Param("token") String authToken,
Expand All @@ -447,8 +411,7 @@ HystrixCommand<KrakenBlockTransaction> blockUser(
*/
@RequestLine("DELETE /users/{from_id}/blocks/{to_id}")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
"Authorization: OAuth {token}"
})
HystrixCommand<Void> unblockUser(
@Param("token") String authToken,
Expand All @@ -468,8 +431,7 @@ HystrixCommand<Void> unblockUser(
*/
@RequestLine("GET /users/{user}/emotes")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
"Authorization: OAuth {token}"
})
HystrixCommand<KrakenEmoticonSetList> getUserEmotes(
@Param("token") String authToken,
Expand All @@ -490,8 +452,7 @@ HystrixCommand<KrakenEmoticonSetList> getUserEmotes(
@Deprecated
@RequestLine("PUT /users/{user}/follows/channels/{targetUser}")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
"Authorization: OAuth {token}"
})
HystrixCommand<Object> addFollow(
@Param("token") String authToken,
Expand All @@ -507,7 +468,6 @@ HystrixCommand<Object> addFollow(
* @return KrakenIngestList
*/
@RequestLine("GET /ingests")
@Headers("Accept: application/vnd.twitchtv.v5+json")
HystrixCommand<KrakenIngestList> getIngestServers();

/**
Expand All @@ -517,7 +477,6 @@ HystrixCommand<Object> addFollow(
* @return KrakenTeamList
*/
@RequestLine("GET /channels/{channel_id}/teams")
@Headers("Accept: application/vnd.twitchtv.v5+json")
HystrixCommand<KrakenTeamList> getChannelTeams(
@Param("channel_id") String channelId
);
Expand All @@ -532,7 +491,6 @@ HystrixCommand<KrakenTeamList> getChannelTeams(
* @return KrakenTeamList
*/
@RequestLine("GET /teams?limit={limit}&offset={offset}")
@Headers("Accept: application/vnd.twitchtv.v5+json")
HystrixCommand<KrakenTeamList> getAllTeams(
@Param("limit") Integer limit,
@Param("offset") Integer offset
Expand All @@ -547,7 +505,6 @@ HystrixCommand<KrakenTeamList> getAllTeams(
* @return KrakenTeam
*/
@RequestLine("GET /teams/{name}")
@Headers("Accept: application/vnd.twitchtv.v5+json")
HystrixCommand<KrakenTeam> getTeamByName(
@Param("name") String name
);
Expand All @@ -562,8 +519,7 @@ HystrixCommand<KrakenTeam> getTeamByName(
*/
@RequestLine("GET /user")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json",
"Authorization: OAuth {token}"
})
HystrixCommand<KrakenUser> getUser(
@Param("token") String authToken
Expand All @@ -576,7 +532,6 @@ HystrixCommand<KrakenUser> getUser(
* @return KrakenUser
*/
@RequestLine("GET /users/{user_id}")
@Headers("Accept: application/vnd.twitchtv.v5+json")
HystrixCommand<KrakenUser> getUserById(
@Param("user_id") String userId
);
Expand All @@ -590,9 +545,6 @@ HystrixCommand<KrakenUser> getUserById(
* @return KrakenUser
*/
@RequestLine(value = "GET /users?login={logins}", collectionFormat = CollectionFormat.CSV)
@Headers({
"Accept: application/vnd.twitchtv.v5+json"
})
HystrixCommand<KrakenUserList> getUsersByLogin(
@Param("logins") List<String> logins
);
Expand All @@ -610,8 +562,7 @@ HystrixCommand<KrakenUserList> getUsersByLogin(
*/
@Deprecated
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json",
"Authorization: OAuth {token}"
})
@RequestLine("PUT /channels/{channelId}?channel[status]={title}")
HystrixCommand<Object> updateTitle(
Expand Down Expand Up @@ -651,8 +602,7 @@ HystrixCommand<Object> updateTitle(
collectionFormat = CollectionFormat.CSV
)
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
"Authorization: OAuth {token}"
})
HystrixCommand<KrakenCreatedVideo> createVideo(
@Param("token") String authToken,
Expand All @@ -668,7 +618,6 @@ HystrixCommand<KrakenCreatedVideo> createVideo(

@RequestLine("PUT /upload/{video_id}?part={part}&upload_token={upload_token}")
@Headers({
"Accept: application/vnd.twitchtv.v5+json",
"Content-Type: application/x-www-form-urlencoded"
})
HystrixCommand<Void> uploadVideoPart(
Expand All @@ -695,7 +644,6 @@ default HystrixCommand<Void> uploadVideoPart(String videoId, String uploadToken,
}

@RequestLine("POST /upload/{video_id}/complete?upload_token={upload_token}")
@Headers("Accept: application/vnd.twitchtv.v5+json")
HystrixCommand<Void> completeVideoUpload(
URI baseUrl,
@Param("video_id") String videoId,
Expand Down Expand Up @@ -734,8 +682,7 @@ default HystrixCommand<Void> completeVideoUpload(String videoId, String uploadTo
collectionFormat = CollectionFormat.CSV
)
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
"Authorization: OAuth {token}"
})
HystrixCommand<KrakenVideo> updateVideo(
@Param("token") String authToken,
Expand All @@ -758,8 +705,7 @@ HystrixCommand<KrakenVideo> updateVideo(
*/
@RequestLine("DELETE /videos/{video_id}")
@Headers({
"Authorization: OAuth {token}",
"Accept: application/vnd.twitchtv.v5+json"
"Authorization: OAuth {token}"
})
HystrixCommand<Void> deleteVideo(
@Param("token") String authToken,
Expand Down

0 comments on commit d68e404

Please sign in to comment.