forked from bunq/sdk_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extracted header-magic as enum (bunq#93)
BunqHeader
- Loading branch information
Showing
5 changed files
with
119 additions
and
125 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
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 |
---|---|---|
@@ -1,26 +1,29 @@ | ||
package com.bunq.sdk.http; | ||
|
||
import okhttp3.Response; | ||
|
||
public class BunqBasicHeader { | ||
private String name; | ||
private String value; | ||
private final BunqHeader name; | ||
private final String value; | ||
|
||
public static BunqBasicHeader get(BunqHeader header,Response response) { | ||
return new BunqBasicHeader(header,response.header(header.getHeader())); | ||
} | ||
|
||
/** | ||
*/ | ||
public BunqBasicHeader(String name, String value) { | ||
this(BunqHeader.parse(name).get(),value); | ||
} | ||
|
||
public BunqBasicHeader(BunqHeader name, String value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
/** | ||
*/ | ||
public String getName() { | ||
public BunqHeader getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
*/ | ||
public String getValue() { | ||
return value; | ||
} | ||
|
||
} |
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,75 @@ | ||
package com.bunq.sdk.http; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
public enum BunqHeader { | ||
attachmentDescription("X-Bunq-Attachment-Description"), | ||
cacheControl("Cache-Control","no-cache"), | ||
contentType("Content-Type"), | ||
clientAuthentication("X-Bunq-Client-Authentication"), | ||
clientEncryptionHMAC("X-Bunq-Client-Encryption-Hmac"), | ||
clientEncryptionIV("X-Bunq-Client-Encryption-Iv"), | ||
clientEncryptionKey("X-Bunq-Client-Encryption-Key"), | ||
clientRequestId("X-Bunq-Client-Request-Id"), | ||
clientSignature("X-Bunq-Client-Signature"), | ||
clientResponseId("X-Bunq-Client-Response-Id"), | ||
geolocation("X-Bunq-Geolocation","0 0 0 0 000"), | ||
language("X-Bunq-Language","en_US"), | ||
region("X-Bunq-Region","nl_NL"), | ||
serverSignature("X-Bunq-Server-Signature"), | ||
userAgent("User-Agent","bunq-sdk-java/0.13.1"); | ||
|
||
private static final String PREFIX = "X-Bunq-"; | ||
|
||
private final String header; | ||
private final String defaultValue; | ||
|
||
BunqHeader(String header) { | ||
this(header,null); | ||
} | ||
|
||
BunqHeader(String header, String defaultValue) { | ||
this.header = header; | ||
this.defaultValue = defaultValue; | ||
} | ||
|
||
public static Optional<BunqHeader> parse(String value) { | ||
return Stream.of(values()).filter(h->h.equals(value)).findAny(); | ||
} | ||
|
||
public String getHeader() { | ||
return header; | ||
} | ||
|
||
public String getDefaultValue() { | ||
return defaultValue; | ||
} | ||
|
||
public void addTo(Map<String,String> headers, String value) { | ||
headers.put(getHeader(),value!=null?value:getDefaultValue()); | ||
} | ||
|
||
public void addTo(BunqRequestBuilder requestBuilder) { | ||
addTo(requestBuilder,null); | ||
} | ||
|
||
public void addTo(BunqRequestBuilder requestBuilder, String value) { | ||
requestBuilder.addHeader(getHeader(),value!=null?value:getDefaultValue()); | ||
} | ||
|
||
public boolean equals(String header) { | ||
return getHeader().equalsIgnoreCase(header); | ||
} | ||
|
||
public boolean isBunq() { | ||
return getHeader().startsWith(PREFIX); | ||
} | ||
|
||
public String getOrDefault(Map<String,String> headers,String defaultValue) { | ||
Optional<String> key = headers.keySet().stream().filter(this::equals).findAny(); | ||
return key.map(headers::get).orElse(defaultValue); | ||
} | ||
} |
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.