/**
 version history
 1.5 2007-12-30 add alternate get and post methods that take a full URL.
 1.6 2008-01-14 add gzip option on read
 */
package com.mindprod.http;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Like A StringBuffer but encodes CGI pairs for GET or POST. See com.mindprod.submitter for sample code to use this
 * class.
 *
 * @author Roedy Green
 * @version 1.6 2008-01-14 add gzip option on read
 * @since 2003-05-26
 */
@SuppressWarnings({ "WeakerAccess" })
public final class Request
    {
    /**
     * used to build the pairs
     */
    private final StringBuilder sb;
// -------------------------- PUBLIC INSTANCE  METHODS --------------------------
    // ideally would extend StringBuilder, but it is final.
    /**
     * constructor
     *
     * @param size estimated size of result string.
     */
    public Request( int size )
        {
        this.sb = new StringBuilder( size );
        }

    /**
     * append a parm=value pair of CGI parameters, ecoding them with URL encoding, xxx=yyy&aaa=bbb etc.
     *
     * @param name  parameter name
     * @param value parameter value
     */
    @SuppressWarnings({ "UnusedDeclaration" })
    public void appendCGIPair( String name, String value )
        {
        if ( sb.length() != 0 )
            {
            // separates pairs
            sb.append( '&' );
            }
        try
            {
            sb.append( URLEncoder.encode( name, "UTF-8" ) );
            sb.append( '=' );
            sb.append( URLEncoder.encode( value, "UTF-8" ) );
            }
        catch ( UnsupportedEncodingException e )
            {
            throw new IllegalArgumentException( "UTF-8 encoding support missing" );
            }
        }

    /**
     * get request as an a URL-encoded String.
     *
     * @return result CGI request string.
     */
    public String toString()
        {
        return sb.toString();
        }
    }