package com.zhy.http.okhttp.builder;

import com.zhy.http.okhttp.request.GetRequest;
import com.zhy.http.okhttp.request.RequestCall;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by zhy on 15/12/14.
 */
public class GetBuilder extends OkHttpRequestBuilder implements HasParamsable
{
    @Override
    public RequestCall build()
    {
        if (params != null)
        {
            url = appendParams(url, params);
        }

        return new GetRequest(url, tag, params, headers).build();
    }

    protected String appendParams(String url, Map<String, String> params)
    {
        StringBuilder sb = new StringBuilder();
        sb.append(url + "?");
        if (params != null && !params.isEmpty())
        {
            for (String key : params.keySet())
            {
                sb.append(key).append("=").append(params.get(key)).append("&");
            }
        }

        sb = sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    @Override
    public GetBuilder url(String url)
    {
        this.url = url;
        return this;
    }

    @Override
    public GetBuilder tag(Object tag)
    {
        this.tag = tag ;
        return this;
    }

    @Override
    public GetBuilder params(Map<String, String> params)
    {
        this.params = params;
        return this;
    }

    @Override
    public GetBuilder addParams(String key, String val)
    {
        if (this.params == null)
        {
            params = new LinkedHashMap<>();
        }
        params.put(key, val);
        return this;
    }

    @Override
    public GetBuilder headers(Map<String, String> headers)
    {
        this.headers = headers;
        return this;
    }

    @Override
    public GetBuilder addHeader(String key, String val)
    {
        if (this.headers == null)
        {
            headers = new LinkedHashMap<>();
        }
        headers.put(key, val);
        return this;
    }
}

@@ -610,110 +610,45 @@
     
- StringBuilder sb = new StringBuilder();%0A        sb.append(url + %22?%22);%0A        if (
+if (url == null %7C%7C 
 params 
-!
+=
 = null 
-&& !
+%7C%7C 
 para
@@ -672,40 +672,127 @@
     
- %7B%0A            for (
+%7B%0A            return url;%0A       %7D%0A        Uri.Builder builder = Uri.parse(url).buildUpon();%0A        Set%3C
 String
+%3E
  key
- :
+s =
  par
@@ -803,17 +803,17 @@
 keySet()
-)
+;
 %0A       
@@ -817,93 +817,87 @@
     
-    %7B%0A                sb.append(key).append(%22=%22).append(params.get(key)).append(%22&%22);
+Iterator%3CString%3E iterator = keys.iterator();%0A        while (iterator.hasNext())
 %0A   
@@ -897,29 +897,25 @@
 ())%0A        
-    %7D
+%7B
 %0A        %7D%0A%0A
@@ -915,57 +915,115 @@
     
-%7D%0A%0A        sb = sb.deleteCharAt(sb.length() - 1);
+    String key = iterator.next();%0A            builder.appendQueryParameter(key, params.get(key));%0A        %7D
 %0A   
@@ -1038,10 +1038,23 @@
 urn 
-s
 b
+uilder.build()
 .toS

fix GET request url build error