-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: set default kraken headers (accept, content-type) using int…
…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
1 parent
eeefaee
commit d68e404
Showing
4 changed files
with
62 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...n/java/com/github/twitch4j/common/feign/interceptor/JsonContentTypeHeaderInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.