Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I have a question and an improvement. #6

Open
guilherme-oliveria opened this issue Mar 16, 2018 · 3 comments
Open

I have a question and an improvement. #6

guilherme-oliveria opened this issue Mar 16, 2018 · 3 comments

Comments

@guilherme-oliveria
Copy link

guilherme-oliveria commented Mar 16, 2018

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

@guilherme-oliveria
Copy link
Author

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

@guilherme-oliveria
Copy link
Author

guilherme-oliveria commented Mar 16, 2018

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 );
						}`

@omandryk
Copy link
Collaborator

omandryk commented Apr 1, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants