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);
}
Aggregations