// Path: src/main/java/com/yelp/clientlib/exception/ErrorHandlingInterceptor.java
// public class ErrorHandlingInterceptor implements Interceptor {
// 
//     private static final ObjectMapper objectMapper = new ObjectMapper();
// 
//     /**
//      * Intercept HTTP responses and raise a {@link YelpAPIError} if the response code is not 2xx.
//      *
//      * @param chain {@link okhttp3.Interceptor.Chain} object for sending the HTTP request.
//      * @return response
//      * @throws IOException {@link YelpAPIError} generated depends on the response error id.
//      */
//     @Override
//     public Response intercept(Chain chain) throws IOException {
//         Response response = chain.proceed(chain.request());
// 
//         if (!response.isSuccessful()) {
//             throw parseError(
//                     response.code(),
//                     response.message(),
//                     response.body() != null ? response.body().string() : null
//             );
//         }
//         return response;
//     }
// 
//     private YelpAPIError parseError(int code, String message, String responseBody) throws IOException {
//         if (responseBody == null) {
//             return new UnexpectedAPIError(code, message);
//         }
// 
//         JsonNode errorJsonNode = objectMapper.readTree(responseBody).path("error");
//         String errorId = errorJsonNode.path("id").asText();
//         String errorText = errorJsonNode.path("text").asText();
// 
//         if (errorJsonNode.has("field")) {
//             errorText += ": " + errorJsonNode.path("field").asText();
//         }
// 
//         switch (errorId) {
//             case "AREA_TOO_LARGE":
//                 return new AreaTooLarge(code, message, errorId, errorText);
//             case "BAD_CATEGORY":
//                 return new BadCategory(code, message, errorId, errorText);
//             case "BUSINESS_UNAVAILABLE":
//                 return new BusinessUnavailable(code, message, errorId, errorText);
//             case "EXCEEDED_REQS":
//                 return new ExceededReqs(code, message, errorId, errorText);
//             case "INTERNAL_ERROR":
//                 return new InternalError(code, message, errorId, errorText);
//             case "INVALID_OAUTH_CREDENTIALS":
//                 return new InvalidOAuthCredentials(code, message, errorId, errorText);
//             case "INVALID_OAUTH_USER":
//                 return new InvalidOAuthUser(code, message, errorId, errorText);
//             case "INVALID_PARAMETER":
//                 return new InvalidParameter(code, message, errorId, errorText);
//             case "INVALID_SIGNATURE":
//                 return new InvalidSignature(code, message, errorId, errorText);
//             case "MISSING_PARAMETER":
//                 return new MissingParameter(code, message, errorId, errorText);
//             case "MULTIPLE_LOCATIONS":
//                 return new MultipleLocations(code, message, errorId, errorText);
//             case "SSL_REQUIRED":
//                 return new SSLRequired(code, message, errorId, errorText);
//             case "UNAVAILABLE_FOR_LOCATION":
//                 return new UnavailableForLocation(code, message, errorId, errorText);
//             case "UNSPECIFIED_LOCATION":
//                 return new UnspecifiedLocation(code, message, errorId, errorText);
//             default:
//                 return new UnexpectedAPIError(code, message, errorId, errorText);
//         }
//     }
// }


import okhttp3.OkHttpClient;
import com.yelp.clientlib.exception.ErrorHandlingInterceptor;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.Retrofit;
import se.akerfeldt.okhttp.signpost.OkHttpOAuthConsumer;
import se.akerfeldt.okhttp.signpost.SigningInterceptor;
package com.yelp.clientlib.connection;




/**
 * Util class to create YelpAPI as the stub to use Yelp API. This is the entry point to use this clientlib.
 * <p>
 * Example:<br>
 * YelpAPIFactory apiFactory = new YelpAPIFactory(consumerKey, consumerSecret, token, tokenSecret);<br>
 * YelpAPI yelpAPI = apiFactory.createAPI();<br>
 * Business business = yelpAPI.getBusiness(businessId).execute();
 * </p>
 */
public class YelpAPIFactory {

    private static final String YELP_API_BASE_URL = "https://api.yelp.com";

    private OkHttpClient httpClient;

    /**
     * Construct a new {@code YelpAPIFactory}.
     *
     * @param consumerKey    the consumer key.
     * @param consumerSecret the consumer secret.
     * @param token          the access token.
     * @param tokenSecret    the token secret.
     * @see <a href="https://www.yelp.com/developers/manage_api_keys">https://www.yelp.com/developers/manage_api_keys</a>
     */
    public YelpAPIFactory(String consumerKey, String consumerSecret, String token, String tokenSecret) {
        OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(consumerKey, consumerSecret);
        consumer.setTokenWithSecret(token, tokenSecret);

        this.httpClient = new OkHttpClient.Builder()
                .addInterceptor(new SigningInterceptor(consumer))
// Path: src/main/java/com/yelp/clientlib/exception/ErrorHandlingInterceptor.java
// public class ErrorHandlingInterceptor implements Interceptor {
// 
//     private static final ObjectMapper objectMapper = new ObjectMapper();
// 
//     /**
//      * Intercept HTTP responses and raise a {@link YelpAPIError} if the response code is not 2xx.
//      *
//      * @param chain {@link okhttp3.Interceptor.Chain} object for sending the HTTP request.
//      * @return response
//      * @throws IOException {@link YelpAPIError} generated depends on the response error id.
//      */
//     @Override
//     public Response intercept(Chain chain) throws IOException {
//         Response response = chain.proceed(chain.request());
// 
//         if (!response.isSuccessful()) {
//             throw parseError(
//                     response.code(),
//                     response.message(),
//                     response.body() != null ? response.body().string() : null
//             );
//         }
//         return response;
//     }
// 
//     private YelpAPIError parseError(int code, String message, String responseBody) throws IOException {
//         if (responseBody == null) {
//             return new UnexpectedAPIError(code, message);
//         }
// 
//         JsonNode errorJsonNode = objectMapper.readTree(responseBody).path("error");
//         String errorId = errorJsonNode.path("id").asText();
//         String errorText = errorJsonNode.path("text").asText();
// 
//         if (errorJsonNode.has("field")) {
//             errorText += ": " + errorJsonNode.path("field").asText();
//         }
// 
//         switch (errorId) {
//             case "AREA_TOO_LARGE":
//                 return new AreaTooLarge(code, message, errorId, errorText);
//             case "BAD_CATEGORY":
//                 return new BadCategory(code, message, errorId, errorText);
//             case "BUSINESS_UNAVAILABLE":
//                 return new BusinessUnavailable(code, message, errorId, errorText);
//             case "EXCEEDED_REQS":
//                 return new ExceededReqs(code, message, errorId, errorText);
//             case "INTERNAL_ERROR":
//                 return new InternalError(code, message, errorId, errorText);
//             case "INVALID_OAUTH_CREDENTIALS":
//                 return new InvalidOAuthCredentials(code, message, errorId, errorText);
//             case "INVALID_OAUTH_USER":
//                 return new InvalidOAuthUser(code, message, errorId, errorText);
//             case "INVALID_PARAMETER":
//                 return new InvalidParameter(code, message, errorId, errorText);
//             case "INVALID_SIGNATURE":
//                 return new InvalidSignature(code, message, errorId, errorText);
//             case "MISSING_PARAMETER":
//                 return new MissingParameter(code, message, errorId, errorText);
//             case "MULTIPLE_LOCATIONS":
//                 return new MultipleLocations(code, message, errorId, errorText);
//             case "SSL_REQUIRED":
//                 return new SSLRequired(code, message, errorId, errorText);
//             case "UNAVAILABLE_FOR_LOCATION":
//                 return new UnavailableForLocation(code, message, errorId, errorText);
//             case "UNSPECIFIED_LOCATION":
//                 return new UnspecifiedLocation(code, message, errorId, errorText);
//             default:
//                 return new UnexpectedAPIError(code, message, errorId, errorText);
//         }
//     }
// }


// Path: src/main/java/com/yelp/clientlib/connection/YelpAPIFactory.java
import okhttp3.OkHttpClient;
import com.yelp.clientlib.exception.ErrorHandlingInterceptor;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.Retrofit;
import se.akerfeldt.okhttp.signpost.OkHttpOAuthConsumer;
import se.akerfeldt.okhttp.signpost.SigningInterceptor;

package com.yelp.clientlib.connection;




/**
 * Util class to create YelpAPI as the stub to use Yelp API. This is the entry point to use this clientlib.
 * <p>
 * Example:<br>
 * YelpAPIFactory apiFactory = new YelpAPIFactory(consumerKey, consumerSecret, token, tokenSecret);<br>
 * YelpAPI yelpAPI = apiFactory.createAPI();<br>
 * Business business = yelpAPI.getBusiness(businessId).execute();
 * </p>
 */
public class YelpAPIFactory {

    private static final String YELP_API_BASE_URL = "https://api.yelp.com";

    private OkHttpClient httpClient;

    /**
     * Construct a new {@code YelpAPIFactory}.
     *
     * @param consumerKey    the consumer key.
     * @param consumerSecret the consumer secret.
     * @param token          the access token.
     * @param tokenSecret    the token secret.
     * @see <a href="https://www.yelp.com/developers/manage_api_keys">https://www.yelp.com/developers/manage_api_keys</a>
     */
    public YelpAPIFactory(String consumerKey, String consumerSecret, String token, String tokenSecret) {
        OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(consumerKey, consumerSecret);
        consumer.setTokenWithSecret(token, tokenSecret);

        this.httpClient = new OkHttpClient.Builder()
                .addInterceptor(new SigningInterceptor(consumer))
                .addInterceptor(new ErrorHandlingInterceptor())