Search in sources :

Example 1 with R

use of io.github.hidroh.materialistic.R in project materialistic by hidroh.

the class UserServicesClient method submit.

@Override
public void submit(Context context, String title, String content, boolean isUrl, Callback callback) {
    Pair<String, String> credentials = AppUtils.getCredentials(context);
    if (credentials == null) {
        callback.onDone(false);
        return;
    }
    /**
         * The flow:
         * POST /submit with acc, pw
         *  if 302 to /login, considered failed
         * POST /r with fnid, fnop, title, url or text
         *  if 302 to /newest, considered successful
         *  if 302 to /x, considered error, maybe duplicate or invalid input
         *  if 200 or anything else, considered error
         */
    // fetch submit page with given credentials
    execute(postSubmitForm(credentials.first, credentials.second)).flatMap(response -> response.code() != HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(response) : Observable.error(new IOException())).flatMap(response -> {
        try {
            return Observable.just(new String[] { response.header(HEADER_SET_COOKIE), response.body().string() });
        } catch (IOException e) {
            return Observable.error(e);
        } finally {
            response.close();
        }
    }).map(array -> {
        array[1] = getInputValue(array[1], SUBMIT_PARAM_FNID);
        return array;
    }).flatMap(array -> !TextUtils.isEmpty(array[1]) ? Observable.just(array) : Observable.error(new IOException())).flatMap(array -> execute(postSubmit(title, content, isUrl, array[0], array[1]))).flatMap(response -> response.code() == HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(Uri.parse(response.header(HEADER_LOCATION))) : Observable.error(new IOException())).flatMap(uri -> TextUtils.equals(uri.getPath(), DEFAULT_SUBMIT_REDIRECT) ? Observable.just(true) : Observable.error(buildException(uri))).observeOn(AndroidSchedulers.mainThread()).subscribe(callback::onDone, callback::onError);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Context(android.content.Context) Request(okhttp3.Request) Uri(android.net.Uri) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) TextUtils(android.text.TextUtils) IOException(java.io.IOException) Scheduler(rx.Scheduler) Observable(rx.Observable) Inject(javax.inject.Inject) AppUtils(io.github.hidroh.materialistic.AppUtils) Matcher(java.util.regex.Matcher) FormBody(okhttp3.FormBody) R(io.github.hidroh.materialistic.R) Pair(android.support.v4.util.Pair) Toast(android.widget.Toast) Response(okhttp3.Response) Call(okhttp3.Call) Pattern(java.util.regex.Pattern) HttpUrl(okhttp3.HttpUrl) IOException(java.io.IOException)

Aggregations

Context (android.content.Context)1 Uri (android.net.Uri)1 Pair (android.support.v4.util.Pair)1 TextUtils (android.text.TextUtils)1 Toast (android.widget.Toast)1 AppUtils (io.github.hidroh.materialistic.AppUtils)1 R (io.github.hidroh.materialistic.R)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Inject (javax.inject.Inject)1 Call (okhttp3.Call)1 FormBody (okhttp3.FormBody)1 HttpUrl (okhttp3.HttpUrl)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 Observable (rx.Observable)1 Scheduler (rx.Scheduler)1 AndroidSchedulers (rx.android.schedulers.AndroidSchedulers)1