wc-api-java
wc-api-java copied to clipboard
I have a question and an improvement.
the search for more than 100 products and more news, for an api brings only 100 in 100 data. this way it becomes more viable.
class WoocommercApi:
`
@Override
@SuppressWarnings("all")
public List getAll(String endpointBase) {
Integer OAUTH_PAGE = 1;
String privado = "private" , publico="publish",
concluido="completed",pendente="pending", cancelado="cancelled",aguardando ="on-hold"
List listaFinal = new ArrayList<>();
if(endpointBase.equals("products")){
listaFinal = allProduct(listaFinal, endpointBase, OAUTH_PAGE, publico);
listaFinal = allProduct(listaFinal, endpointBase, OAUTH_PAGE, privado);
}else if(endpointBase.equals("orders")){
listaFinal = allProduct(listaFinal, endpointBase, OAUTH_PAGE, concluido);
listaFinal = allProduct(listaFinal, endpointBase, OAUTH_PAGE, cancelado);
listaFinal = allProduct(listaFinal, endpointBase, OAUTH_PAGE, pendente);
listaFinal = allProduct(listaFinal, endpointBase, OAUTH_PAGE, aguardando);
}
return listaFinal; // RETORN LIST
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public List allProduct(List listaFinal,String endpointBase, Integer OAUTH_PAGE,String status ) {
List urlAll = client.getAll(metGetAll(endpointBase, OAUTH_PAGE, status)) ;
listaFinal.addAll(urlAll);
while(urlAll !=null) {
OAUTH_PAGE ++;
try{
urlAll = client.getAll(metGetAll(endpointBase, OAUTH_PAGE,status));
listaFinal.addAll(urlAll);
if(urlAll!=null && urlAll.size() == 0){
urlAll = null;
break;
}
}catch(Exception e){
break;
}
}
return listaFinal;
}
public String metGetAll(String endpointBase, Integer OAUTH_PER_PAGE, String status) {
String url = String.format(API_URL_FORMAT, config.getUrl(), endpointBase);
String signature = OauthSignature.getAsQueryStringAll(config, url, HttpMethod.GET, OAUTH_PER_PAGE, status);
String securedUrl = String.format(URL_SECURED_FORMAT, url, signature);
return securedUrl;
}
`
AND CLASS OauthSignature:
`public static Map<String, String> getAsMapAll(OauthConfig config, String endpoint, HttpMethod httpMethod, Integer paginas, String status) {
if (config == null || endpoint == null || httpMethod == null) {
return Collections.emptyMap();
}
String porPaginas ="100"; //page
Map<String,String> params = new HashMap<>();
params.put(OauthHeader.OAUTH_CONSUMER_KEY.getValue(), config.getConsumerKey());
params.put(OauthHeader.OAUTH_CONSUMER_SECRET.getValue(), config.getConsumerSecret());
params.put(OauthHeader.OAUTH_TIMESTAMP.getValue(), String.valueOf(System.currentTimeMillis() / 1000L));
params.put(OauthHeader.OAUTH_NONCE.getValue(), UUID.randomUUID().toString());
params.put(OauthHeader.OAUTH_SIGNATURE_METHOD.getValue(), SIGNATURE_METHOD_HMAC_SHA256);
params.put(OauthHeader.OAUTH_PER_PAGE.getValue(), porPaginas);
params.put(OauthHeader.OAUTH_PAGE.getValue(), paginas.toString());
params.put(OauthHeader.OAUTH_STATUS.getValue(), status); // paramenter
// WooCommerce specified param
if (HttpMethod.DELETE.equals(httpMethod)) {
params.put(DELETE_PARAM_FORCE, Boolean.TRUE.toString());
}
String oAuthSignature = generateOAuthSignature(config.getConsumerSecret(), endpoint, httpMethod, params);
params.put(OauthHeader.OAUTH_SIGNATURE.getValue(), oAuthSignature);
return params;
}
public static String getAsQueryStringAll(OauthConfig config, String endpoint, HttpMethod httpMethod, Integer pags, String status) {
if (config == null || endpoint == null || httpMethod == null) {
return "";
}
Map<String, String> oauthParameters = getAsMapAll(config, endpoint, httpMethod, pags, status);
String encodedSignature = oauthParameters.get(OauthHeader.OAUTH_SIGNATURE.getValue())
.replace(SpecialSymbol.PLUS.getPlain(), SpecialSymbol.PLUS.getEncoded());
oauthParameters.put(OauthHeader.OAUTH_SIGNATURE.getValue(), encodedSignature);
return mapToString(oauthParameters, SpecialSymbol.EQUAL.getPlain(), SpecialSymbol.AMP.getPlain());
}`
Here I look for order and PRODUCTS
I accept improvements on the lists to be searched
my question would be how to use, /wp-json/wc/v2/products/batch
to send the list of products at once because over 1000 data gets very very slow
arrumei um jeito rsrs
`
List<Map<String, Object>> listProdutosBach= new ArrayList<Map<String,Object>>();
List<Map<String, Object>> listaProdutosAuxiliarCriacao = new ArrayList<Map<String,Object>>();
List<Map<String, Object>> listaProdutosAuxiliarAtualizar = new ArrayList<Map<String,Object>>();
for(){ verifica se tem ou adiciona um novo com atributos } if(!produtoEncontrado) {
listaProdutosAuxiliarCriacao.add(setNovoProdutoWecommerc(produtoErp, integracao));
}
}
if(listaProdutosAuxiliarCriacao != null && listaProdutosAuxiliarCriacao.size() >0){
Map<String, Object> produtoEnviar = new HashMap<>();
for(Map<String, Object> mapProduto : listaProdutosAuxiliarCriacao){
listProdutosBach.add(mapProduto);
}
produtoEnviar.put("create",listProdutosBach);
wooCommerce.create(EndpointBaseType.PRODUCTS_BASH.getValue(),produtoEnviar );
}`
Hi @guilherme-oliveria the library currently doesn't support batch operations, but I would be glad to accept a pull request that adds this possibility. Thank you.