Skip to content

Commit

Permalink
formatting feedback, java7 (bunq#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
tubbynl committed Jun 19, 2018
1 parent ad67ada commit 4cd229c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/bunq/sdk/http/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ private BunqResponseRaw createBunqResponseRaw(Response response)
*/
private static String getResponseId(Response response) {
Map<String, String> headerMap = getHeadersMap(response);

return BunqHeader.clientResponseId.getOrDefault(headerMap);
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/bunq/sdk/http/BunqBasicHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import java.util.stream.Stream;

public class BunqBasicHeader {
/**
* String format constants for signing data
*/
private static final String DELIMITER_HEADER_NAME_AND_VALUE = ": ";
private static final String NEWLINE = "\n";

Expand All @@ -17,10 +20,6 @@ public static BunqBasicHeader get(BunqHeader header,Response response) {
return new BunqBasicHeader(header, response.header(header.getHeader()));
}

public static Optional<BunqBasicHeader> get(String header, String value) {
return BunqHeader.parse(header).map(h -> new BunqBasicHeader(h,value));
}

public BunqBasicHeader(BunqHeader name, String value) {
this.name = name;
this.value = value;
Expand Down
38 changes: 31 additions & 7 deletions src/main/java/com/bunq/sdk/http/BunqHeader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bunq.sdk.http;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
Expand Down Expand Up @@ -35,8 +36,14 @@ public enum BunqHeader {
this.defaultValue = defaultValue;
}

public static Optional<BunqHeader> parse(String value) {
return Stream.of(values()).filter(h->h.equals(value)).findAny();
public static BunqHeader parse(String value) {
for (BunqHeader header:values()) {
if (header.equals(value)) {
return header;
}
}

return null;
}

public String getHeader() {
Expand All @@ -47,16 +54,20 @@ public String getDefaultValue() {
return defaultValue;
}

private String getOrDefault(String value) {
return value != null ? value : getDefaultValue();
}

public void addTo(Map<String,String> headers, String value) {
headers.put(getHeader(),value!=null?value:getDefaultValue());
headers.put(getHeader(), getOrDefault(value));
}

public void addTo(BunqRequestBuilder requestBuilder) {
addTo(requestBuilder,null);
}

public void addTo(BunqRequestBuilder requestBuilder, String value) {
requestBuilder.addHeader(getHeader(),value!=null?value:getDefaultValue());
requestBuilder.addHeader(getHeader(), getOrDefault(value));
}

public boolean equals(String header) {
Expand All @@ -67,8 +78,21 @@ public boolean isBunq() {
return getHeader().startsWith(PREFIX);
}

private String findKey(Collection<String> keys) {
for(String key:keys) {
if(this.equals(key)) {
return key;
}
}

return null;
}

public String getOrDefault(Map<String,String> headers) {
Optional<String> key = headers.keySet().stream().filter(this::equals).findAny();
return key.map(headers::get).orElse(getDefaultValue());
String key = findKey(headers.keySet());
if (key != null && headers.get(key) != null) {
return headers.get(key);
}
return getDefaultValue();
}
}
}
14 changes: 12 additions & 2 deletions src/main/java/com/bunq/sdk/http/BunqRequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,21 @@ public BunqRequestBuilder url(URL url) {
return (BunqRequestBuilder) super.url(url);
}

private void addToAllHeader(String name,String value) {
BunqHeader header = BunqHeader.parse(name);

if (header != null) {
this.allHeader.add(new BunqBasicHeader(header, value));
}
}

/**
* {@inheritDoc}
*/
@Override
public BunqRequestBuilder header(String name, String value) {
BunqBasicHeader.get(name,value).ifPresent(this.allHeader::add);
addToAllHeader(name,value);

return (BunqRequestBuilder) super.header(name, value);
}

Expand All @@ -89,7 +98,8 @@ public BunqRequestBuilder header(String name, String value) {
*/
@Override
public BunqRequestBuilder addHeader(String name, String value) {
BunqBasicHeader.get(name,value).ifPresent(this.allHeader::add);
addToAllHeader(name,value);

return (BunqRequestBuilder) super.addHeader(name, value);
}

Expand Down

0 comments on commit 4cd229c

Please sign in to comment.