package com.ejweb.core.push;

import com.ejweb.core.fetcher.FetchEntity;
import com.ejweb.core.fetcher.HCFetcher;


public class MessageClient {

    public MessageClient(String server){
        
    }
    
    public static String post(String server, MessagePayload payload){
        if(payload == null || server == null)
            return null;
        //101.200.146.225  183.62.233.5
        String body = post(server+"api/message", payload.getMessagePayload());
        
        return body;
    }
    public static String post(String server, ProductPayload payload){
        if(payload == null || server == null)
            return null;
        //101.200.146.225  183.62.233.5
        String body = post(server+"api/product", payload.getMessagePayload());
        
        return body;
    }
    public static String post(String spec, byte[] data){
        
        final HCFetcher fetcher = HCFetcher.getInstance();
        FetchEntity entity = fetcher.post(spec, data);
        if(entity.isSuccess() == false){// 如果请求失败则二次请求
            entity = fetcher.post(spec, data);
            if(entity.isSuccess() == false){ // 如果两次请求都失败则返回NULL
                return null;
            }
        }
        return entity.getContent("utf-8");
    }
}
