package com.rejasupotaro.octodroid;

import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;
import com.rejasupotaro.octodroid.http.AbstractClient;
import com.rejasupotaro.octodroid.http.ApiClient;
import com.rejasupotaro.octodroid.http.Method;
import com.rejasupotaro.octodroid.http.Response;
import com.rejasupotaro.octodroid.models.SearchResult;
import com.rejasupotaro.octodroid.models.User;

import org.apache.http.protocol.HTTP;

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

import rx.Observable;

public class GitHubClient extends AbstractClient {
    public GitHubClient(ApiClient apiClient) {
        super(apiClient);
    }

    public Observable<Response<User>> myself() {
        return request(Method.GET, "/user", null, null, new TypeToken<User>() {
        });
    }

    public Observable<Response<SearchResult>> searchRepositories(String q, String sort, String order, int page, int perPage) {
        String path = String.format("/search/repositories?q=%s&sort=%s&order=%s&page=%d&per_page=%d", encode(q), sort, order, page, perPage);
        return request(Method.GET, path, null, null, new TypeToken<SearchResult>() {
        });
    }

    private static String encode(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            return URLEncoder.encode(str, HTTP.UTF_8);
        } catch (UnsupportedEncodingException e) {
            // Should not occur
            return "";
        }
    }
}


@@ -542,16 +542,43 @@
 ervable;
+%0Aimport rx.functions.Func1;
 %0A%0Apublic
@@ -915,16 +915,22 @@
 itories(
+final 
 String q
@@ -930,16 +930,22 @@
 tring q,
+ final
  String 
@@ -949,16 +949,22 @@
 ng sort,
+ final
  String 
@@ -969,16 +969,22 @@
 g order,
+ final
  int pag
@@ -985,16 +985,22 @@
 nt page,
+ final
  int per
@@ -1230,24 +1230,317 @@
 Result%3E() %7B%0A
+        %7D).map(new Func1%3CResponse%3CSearchResult%3E, Response%3CSearchResult%3E%3E() %7B%0A            @Override%0A            public Response%3CSearchResult%3E call(Response%3CSearchResult%3E r) %7B%0A                r.next(searchRepositories(q, sort, order, page + 1, perPage));%0A                return r;%0A            %7D%0A
         %7D);%0A

Set next