/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Abhijit Parida <abhijitparida.me@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package app.abhijit.iter.data;

import android.content.Context;
import android.os.AsyncTask;

import java.util.Formatter;

import app.abhijit.iter.R;
import app.abhijit.iter.exceptions.ConnectionFailedException;
import app.abhijit.iter.exceptions.InvalidCredentialsException;
import app.abhijit.iter.exceptions.InvalidResponseException;
import app.abhijit.iter.models.Student;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * This class fetches data from the API.
 */
public class IterApi {

    private String baseUrl;
    private String registrationId;

    public IterApi(Context context) {
        this.baseUrl = context.getResources().getString(R.string.api_base_url);
        this.registrationId = context.getResources().getString(R.string.registration_id);
    }

    public void getStudent(String username, String password, Callback callback) {
        new AsyncTask<Object, Void, Object[]>() {

            @Override
            protected Object[] doInBackground(Object... params) {
                String username = (String) params[0];
                String password = (String) params[1];
                Callback callback = (Callback) params[2];
                CookieJar cookieJar = new CookieJar();
                XsrfToken xsrfToken = new XsrfToken(cookieJar);
                OkHttpClient okHttpClient = new OkHttpClient.Builder()
                        .cookieJar(cookieJar)
                        .addInterceptor(xsrfToken)
                        .build();
                String baseUrl = (String) params[3];
                String registrationId = (String) params[4];
                MediaType json = MediaType.parse("application/json");

                String loginRequestBody = new Formatter()
                        .format("{\"username\":\"%s\",\"password\":\"%s\"}",
                                username, password).toString();
                Request loginRequest = new Request.Builder()
                        .url(baseUrl + "/login")
                        .post(RequestBody.create(json, loginRequestBody))
                        .build();

                String attendanceRequestBody = new Formatter()
                        .format("{\"registerationid\":\"%s\"}", registrationId).toString();
                Request attendanceRequest = new Request.Builder()
                        .url(baseUrl + "/attendanceinfo")
                        .post(RequestBody.create(json, attendanceRequestBody))
                        .build();

                try {
                    String loginJson = okHttpClient.newCall(loginRequest)
                            .execute().body().toString();
                    String attendanceJson = okHttpClient.newCall(attendanceRequest)
                            .execute().body().toString();
                    Student student = new ResponseParser().parse(loginJson, attendanceJson);
                    student.username = username;
                    student.password = password;
                    return new Object[]{student, null, callback};
                } catch (InvalidCredentialsException | InvalidResponseException e) {
                    return new Object[]{null, e, callback};
                } catch (Exception e) {
                    return new Object[]{null, new ConnectionFailedException(), callback};
                }
            }

            @Override
            protected void onPostExecute(Object[] result) {
                Student student = (Student) result[0];
                RuntimeException error = (RuntimeException) result[1];
                Callback callback = (Callback) result[2];

                if (callback != null) {
                    if (error != null) {
                        callback.onError(error);
                    } else {
                        callback.onData(student);
                    }
                }
            }
        }.execute(username, password, callback, this.baseUrl, this.registrationId);
    }

    public interface Callback {

        public void onData(Student student);

        public void onError(RuntimeException error);
    }
}

@@ -3844,35 +3844,33 @@
 xecute().body().
-toS
+s
 tring();%0A       
@@ -3992,19 +3992,17 @@
 .body().
-toS
+s
 tring();

Fix network requests