All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.amadeus.Params Maven / Gradle / Ivy

package com.amadeus;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import lombok.NonNull;

/**
 * A convenient helper class for building data to pass into a request.
 *
 * 
 *   amadeus.get("/foo/bar", Params.with("first_name", "John").and("last_name", "Smith"));
 * 
*/ public class Params extends HashMap { protected String encoding = "UTF-8"; protected Params() {} /** * Initializes a new Param map with an initial key/value pair. * *
   *   amadeus.get("/foo/bar", Params.with("first_name", "John"));
   * 
* * @param key the key for the parameter to send to the API * @param value the value for the given key * @return the Param object, allowing for convenient chaining */ public static Params with(@NonNull String key, Object value) { return new Params().and(key, value); } /** * Adds another key/value pair to the Params map. Automatically * converts all values to strings. * *
   *   amadeus.get("/foo/bar", Params.with("first_name", "John").and("last_name", "Smith"));
   * 
* * @param key the key for the parameter to send to the API * @param value the value for the given key * @return the Param object, allowing for convenient chaining */ public Params and(@NonNull String key, Object value) { put(key, String.valueOf(value)); return this; } // Converts params into a HTTP query string. protected String toQueryString() { StringBuilder query = new StringBuilder(); boolean first = true; for (Map.Entry entry : entrySet()) { if (!first) { query.append("&"); } first = false; try { query.append(URLEncoder.encode(entry.getKey(), encoding)); query.append("="); query.append(URLEncoder.encode(entry.getValue(), encoding)); } catch (UnsupportedEncodingException e) { // no need to anything } } return query.toString(); } /** * Converts params into a HTTP query string. */ public String toString() { return toQueryString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy