<commit_before>/**
 * 
 */
package com.mcac0006.siftscience;

import java.io.IOException;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

import com.mcac0006.siftscience.event.domain.Event;
import com.mcac0006.siftscience.exception.SiftScienceException;
import com.mcac0006.siftscience.label.domain.Label;
import com.mcac0006.siftscience.partner.domain.SiftMerchant;
import com.mcac0006.siftscience.result.domain.SiftMerchantResponse;
import com.mcac0006.siftscience.result.domain.SiftScienceResponse;
import com.mcac0006.siftscience.score.domain.SiftScienceScore;

/**
 * This helper will take care of marshalling the content you wish to send to Sift Science and 
 * also POST send it to Sift Science.
 * 
 * <strong>This class is synchronous.</strong>
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 *
 */
public class SiftScienceHelper {
	
	private static ObjectMapper mapper;
	private static String PATH_EVENTS_API = "https://api.siftscience.com/v203/events";
	private static String PATH_SCORE_API = "https://api.siftscience.com/v203/score/";
	private static String PATH_LABELS_API = "https://api.siftscience.com/v203/users/";
	private static String PATH_PARTNERS_API = "https://api3.siftscience.com/v3/partners/";	
	private static String PATH_DEVICE_FINGERPRINTING_API = "";
	
	static {
		mapper = new ObjectMapper();
		mapper.setSerializationInclusion(Inclusion.NON_NULL);
	}

	private SiftScienceHelper() {}
	
	/**
	 * Sends an event ($transaction, $create_account, etc ...) to Sift Science.
	 * 
	 * @param event - the content regarding the user (or session) in question.
	 * @return the Sift Science response which denotes whether the request has been processed successfully or not.
	 */
	public static SiftScienceResponse send(final Event event) {
		
		try {
			
			final Client client = ClientBuilder.newClient();
			final WebTarget target = client.target(PATH_EVENTS_API);
			final Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
			final Response post = request.post(Entity.entity(serialize(event), MediaType.APPLICATION_JSON_TYPE));
			
			final SiftScienceResponse siftResult = deserializeResponse(post.readEntity(String.class));
			return siftResult;
			
		} catch (IOException e) {
			throw new SiftScienceException("Error generating JSON content to send event.", e);
		}
	}
	
	/**
	 * Sends a Label ($label) to Sift Science.
	 * 
	 * @param userId - the user in question
	 * @param label - the content regarding the user in question.
	 * @return the Sift Science response which denotes whether the request has been processed successfully or not.
	 */
	public static SiftScienceResponse send(final String userId, final Label label) {
		
		try {
			
			final Client client = ClientBuilder.newClient();
			final WebTarget target = client.target(PATH_LABELS_API).path(userId).path("labels");
			final Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
			final Response post = request.post(Entity.entity(serialize(label), MediaType.APPLICATION_JSON_TYPE));
			
			final SiftScienceResponse siftResult = deserializeResponse(post.readEntity(String.class));
			return siftResult;
			
		} catch (IOException e) {
			throw new SiftScienceException("Error generating JSON content to send label.", e);
		}
	}

	/**
	 * Sends a create account request ($accounts) to Sift Science.
	 * 
	 * @param partnerId - the partner in question
	 * @param apikey - the api key to denote which Sift Science account to use.
	 * @param merchantAccount - the entity of Sift Science merchant account to create.

	 * @return the Sift Science response with the account created.
	 */
	public static SiftMerchantResponse createAccount(final String partnerId, final String apikey, final SiftMerchant merchantAccount) {
		
		try {
			
			final Client client = ClientBuilder.newClient().register(new Authenticator(apikey,""));
			final WebTarget target = client.target(PATH_PARTNERS_API).path(partnerId).path("/accounts");
			final Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
			final Response post = request.post(Entity.entity(serialize(merchantAccount), MediaType.APPLICATION_JSON_TYPE));
			
			final SiftMerchantResponse siftMerchantResult = deserializeMerchantResponse(post.readEntity(String.class));
			return siftMerchantResult;
			
		} catch (IOException e) {
			throw new SiftScienceException("Error generating JSON content to createAccount " + e.getMessage(), e);
		}
	}

	public static String listAccounts(final String partnerId, final String apikey) {
		
			final Client client = ClientBuilder.newClient().register(new Authenticator(apikey,""));
			final WebTarget target = client.target(PATH_PARTNERS_API).path(partnerId).path("/accounts");
			final Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
			final Response get = request.get();
			
			return get.readEntity(String.class);
			
	}

	/**
	 * Retrieve a risk assessment of a particular user. This is particularly useful to consult with Sift Science 
	 * before you proceed with any (user-invoked or system-invoked) operations (such as a purchase) on that user.
	 * 
	 * @param api_key - the api key to denote which Sift Science account to use.
	 * @param userId - the user would you like to run a risk assessment on.
	 * 
	 * @return a Sift Science score wrapped in a {@link SiftScienceScore} instance containing information such as the 
	 *         fraud score and the reason. 
	 *         
	 *         Refer to the class' JavaDocs for more information.
	 */
	public static SiftScienceScore getScore(final String api_key, final String userId) {
		
		try {
			
			final Client client = ClientBuilder.newClient();
			final WebTarget target = client.target(PATH_SCORE_API).path(userId).queryParam("api_key", api_key);
			final Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
			final Response get = request.get();
			
			final SiftScienceScore score = deserializeScore(get.readEntity(String.class));
			return score;
			
		} catch (IOException e) {
			throw new RuntimeException("Error generating JSON content to retrieve score request.", e);<commit_msg>Se agrega método para obtener información de dispositivos<commit_after>/**
 * 
 */
package com.mcac0006.siftscience;

import java.io.IOException;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

import com.mcac0006.siftscience.event.domain.Event;
import com.mcac0006.siftscience.exception.SiftScienceException;
import com.mcac0006.siftscience.label.domain.Label;
import com.mcac0006.siftscience.partner.domain.SiftMerchant;
import com.mcac0006.siftscience.result.domain.SiftMerchantResponse;
import com.mcac0006.siftscience.result.domain.SiftScienceResponse;
import com.mcac0006.siftscience.score.domain.SiftScienceScore;

/**
 * This helper will take care of marshalling the content you wish to send to Sift Science and 
 * also POST send it to Sift Science.
 * 
 * <strong>This class is synchronous.</strong>
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 *
 */
public class SiftScienceHelper {
	
	private static ObjectMapper mapper;
	private static String PATH_EVENTS_API = "https://api.siftscience.com/v203/events";
	private static String PATH_SCORE_API = "https://api.siftscience.com/v203/score/";
	private static String PATH_LABELS_API = "https://api.siftscience.com/v203/users/";
	private static String PATH_PARTNERS_API = "https://api3.siftscience.com/v3/partners/";	
	private static String PATH_DEVICE_FINGERPRINTING_API = "https://api3.siftscience.com/v3/accounts/";
	
	static {
		mapper = new ObjectMapper();
		mapper.setSerializationInclusion(Inclusion.NON_NULL);
	}

	private SiftScienceHelper() {}
	
	/**
	 * Sends an event ($transaction, $create_account, etc ...) to Sift Science.
	 * 
	 * @param event - the content regarding the user (or session) in question.
	 * @return the Sift Science response which denotes whether the request has been processed successfully or not.
	 */
	public static SiftScienceResponse send(final Event event) {
		
		try {
			
			final Client client = ClientBuilder.newClient();
			final WebTarget target = client.target(PATH_EVENTS_API);
			final Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
			final Response post = request.post(Entity.entity(serialize(event), MediaType.APPLICATION_JSON_TYPE));
			
			final SiftScienceResponse siftResult = deserializeResponse(post.readEntity(String.class));
			return siftResult;
			
		} catch (IOException e) {
			throw new SiftScienceException("Error generating JSON content to send event.", e);
		}
	}
	
	/**
	 * Sends a Label ($label) to Sift Science.
	 * 
	 * @param userId - the user in question
	 * @param label - the content regarding the user in question.
	 * @return the Sift Science response which denotes whether the request has been processed successfully or not.
	 */
	public static SiftScienceResponse send(final String userId, final Label label) {
		
		try {
			
			final Client client = ClientBuilder.newClient();
			final WebTarget target = client.target(PATH_LABELS_API).path(userId).path("labels");
			final Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
			final Response post = request.post(Entity.entity(serialize(label), MediaType.APPLICATION_JSON_TYPE));
			
			final SiftScienceResponse siftResult = deserializeResponse(post.readEntity(String.class));
			return siftResult;
			
		} catch (IOException e) {
			throw new SiftScienceException("Error generating JSON content to send label.", e);
		}
	}

	/**
	 * Sends a create account request ($accounts) to Sift Science.
	 * 
	 * @param partnerId - the partner in question
	 * @param apikey - the api key to denote which Sift Science account to use.
	 * @param merchantAccount - the entity of Sift Science merchant account to create.

	 * @return the Sift Science response with the account created.
	 */
	public static SiftMerchantResponse createAccount(final String partnerId, final String apikey, final SiftMerchant merchantAccount) {
		
		try {
			
			final Client client = ClientBuilder.newClient().register(new Authenticator(apikey,""));
			final WebTarget target = client.target(PATH_PARTNERS_API).path(partnerId).path("/accounts");
			final Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
			final Response post = request.post(Entity.entity(serialize(merchantAccount), MediaType.APPLICATION_JSON_TYPE));
			
			final SiftMerchantResponse siftMerchantResult = deserializeMerchantResponse(post.readEntity(String.class));
			return siftMerchantResult;
			
		} catch (IOException e) {
			throw new SiftScienceException("Error generating JSON content to createAccount " + e.getMessage(), e);
		}
	}

	public static String listAccounts(final String partnerId, final String apikey) {
		
			final Client client = ClientBuilder.newClient().register(new Authenticator(apikey,""));
			final WebTarget target = client.target(PATH_PARTNERS_API).path(partnerId).path("/accounts");
			final Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
			final Response get = request.get();
			
			return get.readEntity(String.class);
			
	}
	
	/**
	 * Sends a getSession request to Sift Science.
	 * 
	 * @param accountId - the sift account in question
	 * @param apikey - the api key to denote which Sift Science account to use.
	 * @param sessionId - the sessionId of your server session.

	 * @return the Sift Science response with the device info of the session.
	 */
	public static String getSession( final String accountId, final String apikey, String sessionId ){
		
		final Client client = ClientBuilder.newClient().register(new Authenticator(apikey,""));
		final WebTarget target = client.target(PATH_DEVICE_FINGERPRINTING_API).path(accountId).path("/sessions").path(sessionId);
		final Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
		final Response get = request.get();
		
		return get.readEntity(String.class);
	}

	/**
	 * Retrieve a risk assessment of a particular user. This is particularly useful to consult with Sift Science 
	 * before you proceed with any (user-invoked or system-invoked) operations (such as a purchase) on that user.
	 * 
	 * @param api_key - the api key to denote which Sift Science account to use.
	 * @param userId - the user would you like to run a risk assessment on.
	 * 
	 * @return a Sift Science score wrapped in a {@link SiftScienceScore} instance containing information such as the 
	 *         fraud score and the reason. 
	 *         
	 *         Refer to the class' JavaDocs for more information.
	 */
	public static SiftScienceScore getScore(final String api_key, final String userId) {
		
		try {
			
			final Client client = ClientBuilder.newClient();
			final WebTarget target = client.target(PATH_SCORE_API).path(userId).queryParam("api_key", api_key);
			final Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
			final Response get = request.get();
			
			final SiftScienceScore score = deserializeScore(get.readEntity(String.class));
			return score;
			
		} catch (IOException e) {
			throw new RuntimeException("Error generating JSON content to retrieve score request.", e);
{'fraction_non_alphanumeric': 0.09454649010394418, 'fraction_numerical': 0.0062651288623095545, 'mean_word_length': 4.42774343122102, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 9, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6078042', 'n_tokens_mistral': 4459, 'n_tokens_neox': 3921, 'n_words': 1157}