Last active
February 19, 2018 14:51
-
-
Save americanstone/74cb2f07569698e2fe28adf01b598946 to your computer and use it in GitHub Desktop.
Better way to construct REST request params?
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
package com.dealer.modules.cms.promotions.factory; | |
import java.util.AbstractMap; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Locale; | |
import java.util.Map; | |
import static java.util.stream.Collectors.toList; | |
import static java.util.stream.Collectors.toMap; | |
public class RestROFactory { | |
// the enum represent the query string to filter down the promotions. | |
// enum give a nice way to see all the params instead of a map you have to know all the keys | |
public enum QueryRequestEnum { | |
ACCOUNT_ID("accountId", ""), //required | |
IS_ACTIVE("isActive",true), //optional | |
TYPE("type", Arrays.asList(PromotionType.VEHICLE)),//optional, w/o type get all types result back | |
SORT("sort", "-dateCreated"),//optional | |
INCLUDE_AUTO_GENERATED("includeAutoGenerated", false),//optional | |
LOCALE("locale", Locale.US), | |
PROMO_IDS("promotionId", ""); | |
private final String key; | |
private Object value; | |
QueryRequestEnum(String k, Object v){ | |
this.key = k; | |
this.value = v; | |
} | |
public String key(){ | |
return key; | |
} | |
public Object getValue() { | |
return value; | |
} | |
public QueryRequestEnum setValue(Object o) { | |
this.value = o; | |
return this; | |
} | |
//consumers(banner,listing,slideshow) create enum request list this method convert it into rest query string | |
public static Map<String, List<String>> buildResourceQueryParams(List<RestROFactory.QueryRequestEnum> requestEnums) { | |
Map<String, List<String>> queryParams = requestEnums.stream() | |
.collect(toMap(RestROFactory.QueryRequestEnum::key, RestROFactory.QueryRequestEnum::getValue)) | |
.entrySet().stream() | |
.map(e -> { | |
if(e.getValue() instanceof List<?>){ | |
return new AbstractMap.SimpleEntry<>(e.getKey(), ((List<?>)e.getValue()).stream().map(i -> i.toString()).collect(toList())); | |
}else{ | |
return new AbstractMap.SimpleEntry<>(e.getKey(), Arrays.asList(e.getValue().toString())); | |
} | |
}).collect(toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); | |
return queryParams; | |
} | |
} | |
public static PromoRO createPromoRO(Placement placement, List<QueryRequestEnum> requestEnums){ | |
return new PromoRO(placement, requestEnums); | |
} | |
public static class PromoRO { | |
private String accountId; | |
private Locale locale; | |
private Placement placement; | |
private List<QueryRequestEnum> requestEnums; | |
private PromoRO(Placement placement, List<QueryRequestEnum> requestEnums) { | |
if(requestEnums.contains(QueryRequestEnum.ACCOUNT_ID)){ | |
this.accountId = requestEnums.get(requestEnums.indexOf(QueryRequestEnum.ACCOUNT_ID)).getValue().toString(); | |
}else { | |
throw new IllegalArgumentException(QueryRequestEnum.ACCOUNT_ID + " is required."); | |
} | |
if(requestEnums.contains(QueryRequestEnum.LOCALE)){ | |
this.locale = (Locale)requestEnums.get(requestEnums.indexOf(QueryRequestEnum.LOCALE)).getValue(); | |
}else{ | |
this.locale = Locale.US; | |
} | |
this.placement = placement; | |
this.requestEnums = requestEnums; | |
} | |
public String getAccountId() { | |
return accountId; | |
} | |
public Locale getLocale() { return locale; } | |
public Placement getPlacement() { | |
return placement; | |
} | |
public Map<String, List<String>> getQueryStrings(){ | |
return QueryRequestEnum.buildResourceQueryParams(requestEnums); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment