// Path: src/main/java/com/rapid7/appspider/Utility.java
// public static boolean isSuccessStatusCode(HttpResponse response) {
//     if (Objects.isNull(response))
//         return false;
// 
//     // https://www.w3.org/Protocols/HTTP/HTRESP.html
//     // while most if not all AppSpider Enterprise endpoints return 200 on success it's
//     // safer to treat any success code as success
//     int statusCode = response.getStatusLine().getStatusCode();
//     return statusCode >= 200 && statusCode <= 299;
// }


import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.json.JSONObject;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import static com.rapid7.appspider.Utility.isSuccessStatusCode;
     * @return Optional of HttpPost containg the request object
     */
    @Override
    public Optional<HttpPost> buildPostRequestUsingApplicationJson(String endpoint, HttpEntity body) {
        ensureArgumentsValid(endpoint, body);

        HttpPost request = new HttpPost(endpoint);
        request.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
        request.setEntity(body);
        return Optional.of(request);
    }

    /**
     * builds a HttpPost request object for the given endpoint containing the provided body content
     * body will be posted using Content-Type as application/x-www-form-urlencoded
     * @param endpoint endpoint to perform post request on
     * @param authToken authorization token for basic authentication
     * @param params name/value pairs sent as the entity of the request
     * @return Optional of HttpPost containg the request object
     */
    @Override
    public Optional<HttpPost> buildPostRequestUsingFormUrlEncoding(String endpoint, String authToken, NameValuePair... params) {
        ensureArgumentsValid(endpoint, authToken);

        HttpPost request = new HttpPost(endpoint);
        request.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED);
        request.addHeader(HttpHeaders.AUTHORIZATION, String.format(BASIC_AUTHENTICATION_FORMAT, authToken));

        try {
            request.setEntity(new UrlEncodedFormEntity(Arrays.asList(params)));
// Path: src/main/java/com/rapid7/appspider/Utility.java
// public static boolean isSuccessStatusCode(HttpResponse response) {
//     if (Objects.isNull(response))
//         return false;
// 
//     // https://www.w3.org/Protocols/HTTP/HTRESP.html
//     // while most if not all AppSpider Enterprise endpoints return 200 on success it's
//     // safer to treat any success code as success
//     int statusCode = response.getStatusLine().getStatusCode();
//     return statusCode >= 200 && statusCode <= 299;
// }


// Path: src/main/java/com/rapid7/appspider/HttpClientService.java
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.json.JSONObject;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import static com.rapid7.appspider.Utility.isSuccessStatusCode;

     * @return Optional of HttpPost containg the request object
     */
    @Override
    public Optional<HttpPost> buildPostRequestUsingApplicationJson(String endpoint, HttpEntity body) {
        ensureArgumentsValid(endpoint, body);

        HttpPost request = new HttpPost(endpoint);
        request.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
        request.setEntity(body);
        return Optional.of(request);
    }

    /**
     * builds a HttpPost request object for the given endpoint containing the provided body content
     * body will be posted using Content-Type as application/x-www-form-urlencoded
     * @param endpoint endpoint to perform post request on
     * @param authToken authorization token for basic authentication
     * @param params name/value pairs sent as the entity of the request
     * @return Optional of HttpPost containg the request object
     */
    @Override
    public Optional<HttpPost> buildPostRequestUsingFormUrlEncoding(String endpoint, String authToken, NameValuePair... params) {
        ensureArgumentsValid(endpoint, authToken);

        HttpPost request = new HttpPost(endpoint);
        request.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED);
        request.addHeader(HttpHeaders.AUTHORIZATION, String.format(BASIC_AUTHENTICATION_FORMAT, authToken));

        try {
            request.setEntity(new UrlEncodedFormEntity(Arrays.asList(params)));
            return Optional.of(request);