Skip to content

Commit

Permalink
Change all POST requests to retrofit2
Browse files Browse the repository at this point in the history
Note: Polls were not tested, since they don't exist.
  • Loading branch information
meiron03 committed Jan 26, 2025
1 parent 0c40429 commit 581955b
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 291 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pennapps.labs.pennmobile.api

import StudentLifeRf2
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
Expand All @@ -11,15 +12,13 @@ import android.webkit.WebViewClient
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import com.pennapps.labs.pennmobile.MainActivity
import com.pennapps.labs.pennmobile.R
import com.pennapps.labs.pennmobile.databinding.FragmentHuntsmanGsrloginBinding
import com.pennapps.labs.pennmobile.gsr.classes.GSRBookingResult
import com.pennapps.labs.pennmobile.gsr.fragments.GsrTabbedFragment
import retrofit.Callback
import retrofit.RetrofitError
import retrofit.client.Response
import kotlinx.coroutines.launch

class HuntsmanGSRLogin : Fragment() {
// gsr details
Expand All @@ -31,6 +30,7 @@ class HuntsmanGSRLogin : Fragment() {
private var gid: Int = 0

private lateinit var mStudentLife: StudentLife
private lateinit var mStudentLifeRf2: StudentLifeRf2
private lateinit var mActivity: MainActivity

private var _binding: FragmentHuntsmanGsrloginBinding? = null
Expand All @@ -39,6 +39,7 @@ class HuntsmanGSRLogin : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mStudentLife = MainActivity.studentLifeInstance
mStudentLifeRf2= MainActivity.studentLifeInstanceRf2
mActivity = activity as MainActivity
arguments?.let { arguments ->
gsrID = arguments.getString("gsrID") ?: ""
Expand Down Expand Up @@ -124,68 +125,64 @@ class HuntsmanGSRLogin : Fragment() {
sessionID: String,
) {
(activity as MainActivity).mNetworkManager.getAccessToken {
try {
mStudentLife.bookGSR(
// Passing the values
bearerToken,
startTime,
endTime,
gid,
Integer.parseInt(gsrID),
roomName,
// Creating an anonymous callback
object : Callback<GSRBookingResult> {
override fun success(
result: GSRBookingResult?,
response: Response?,
) {
// Display the output as a toast
if (result?.getResults() == true) {
Toast
.makeText(mActivity, "GSR successfully booked", Toast.LENGTH_LONG)
.show()
} else {
Log.e("HuntsmanGSRLogin", "GSR booking failed: " + result?.getError())
Toast
.makeText(mActivity, "GSR booking failed", Toast.LENGTH_LONG)
.show()
val sp = PreferenceManager.getDefaultSharedPreferences(mActivity)
val editor = sp.edit()
editor.remove(getString(R.string.huntsmanGSR_SessionID))
editor.apply()
}
// redirect user
val gsrFragment = GsrTabbedFragment()
val fragmentManager = mActivity.supportFragmentManager
fragmentManager
.beginTransaction()
.replace(R.id.content_frame, gsrFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}

override fun failure(error: RetrofitError?) {
// If any error occurred display the error as toast
Log.e("HuntsmanGSRLogin", "GSR booking failed" + error.toString())
Toast.makeText(mActivity, "GSR booking failed", Toast.LENGTH_LONG).show()
viewLifecycleOwner.lifecycleScope.launch {
try {
val response = mStudentLifeRf2.bookGSR(
// Passing the values
bearerToken,
startTime,
endTime,
gid,
Integer.parseInt(gsrID),
roomName
)
if (response.isSuccessful) {
val result = response.body()
if (result?.getResults() == true) {
Toast
.makeText(mActivity, "GSR successfully booked", Toast.LENGTH_LONG)
.show()
} else {
Log.e("HuntsmanGSRLogin", "GSR booking failed: " + result?.getError())
Toast
.makeText(mActivity, "GSR booking failed", Toast.LENGTH_LONG)
.show()
val sp = PreferenceManager.getDefaultSharedPreferences(mActivity)
val editor = sp.edit()
editor.remove(getString(R.string.huntsmanGSR_SessionID))
editor.apply()
// redirect user
val gsrFragment = GsrTabbedFragment()
val fragmentManager = mActivity.supportFragmentManager
fragmentManager
.beginTransaction()
.replace(R.id.content_frame, gsrFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
},
)
} catch (e: Exception) {
e.printStackTrace()
// redirect user
val gsrFragment = GsrTabbedFragment()
val fragmentManager = mActivity.supportFragmentManager
fragmentManager
.beginTransaction()
.replace(R.id.content_frame, gsrFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
} else {
val error = Exception(response.errorBody().toString())
Log.e("HuntsmanGSRLogin", "GSR booking failed $error")
Toast.makeText(mActivity, "GSR booking failed", Toast.LENGTH_LONG).show()
val sp = PreferenceManager.getDefaultSharedPreferences(mActivity)
val editor = sp.edit()
editor.remove(getString(R.string.huntsmanGSR_SessionID))
editor.apply()
// redirect user
val gsrFragment = GsrTabbedFragment()
val fragmentManager = mActivity.supportFragmentManager
fragmentManager
.beginTransaction()
.replace(R.id.content_frame, gsrFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
} catch (e: Exception) {
e.printStackTrace()
}

}

}
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
package com.pennapps.labs.pennmobile.api;

import com.pennapps.labs.pennmobile.api.classes.Account;
import com.pennapps.labs.pennmobile.dining.classes.DiningHall;
import com.pennapps.labs.pennmobile.dining.classes.DiningRequest;
import com.pennapps.labs.pennmobile.fling.classes.FlingEvent;
import com.pennapps.labs.pennmobile.gsr.classes.GSRBookingResult;
import com.pennapps.labs.pennmobile.gsr.classes.GSRLocation;
import com.pennapps.labs.pennmobile.gsr.classes.GSRReservation;
import com.pennapps.labs.pennmobile.home.classes.Poll;
import com.pennapps.labs.pennmobile.home.classes.Post;
import com.pennapps.labs.pennmobile.api.classes.SaveAccountResponse;
import com.pennapps.labs.pennmobile.dining.classes.Venue;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.client.Response;
import retrofit.http.Body;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
import retrofit.http.GET;
import retrofit.http.Header;
import retrofit.http.Headers;
import retrofit.http.POST;
import retrofit.http.Path;
import rx.Observable;

Expand All @@ -47,17 +34,6 @@ Observable<DiningHall> daily_menu(
@GET("/gsr/locations")
Observable<List<GSRLocation>> location ();

@FormUrlEncoded
@POST("/gsr/book/")
void bookGSR(
@Header("Authorization") String bearerToken,
@Field("start_time") String start,
@Field("end_time") String end,
@Field("gid") int gid,
@Field("id") int id,
@Field("room_name") String roomName,
Callback<GSRBookingResult> callback);

@GET("/events/fling")
Observable<List<FlingEvent>> getFlingEvents();

Expand All @@ -66,52 +42,12 @@ Observable<List<GSRReservation>> getGsrReservations(
@Header("Authorization") String bearerToken
);

@FormUrlEncoded
@POST("/gsr/cancel/")
void cancelReservation(
@Header("Authorization") String bearerToken,
@Header("X-Device-ID") String deviceID,
@Field("booking_id") String bookingID,
@Field("sessionid") String sessionID,
Callback<Response> callback);

// accounts
@Headers({"Content-Type: application/json"})
@POST("/users/{pennkey}/activate/")
void saveAccount(
@Header("Authorization") String bearerToken,
@Path("pennkey") String pennkey,
@Body Account account,
Callback<SaveAccountResponse> callback);

@GET("/laundry/preferences")
Observable<List<Integer>> getLaundryPref(
@Header("Authorization") String bearerToken);

@Headers({"Content-Type: application/json"})
@POST("/dining/preferences/")
void sendDiningPref(
@Header("Authorization") String bearerToken,
@Body DiningRequest body,
Callback<Response> callback);

@GET("/portal/posts/browse/")
Observable<List<Post>> validPostsList(
@Header("Authorization") String bearerToken
);

@FormUrlEncoded
@POST("/portal/polls/browse/")
Observable<List<Poll>> browsePolls(
@Header("Authorization") String bearerToken,
@Field("id_hash") String idHash
);

@FormUrlEncoded
@POST("/portal/votes/")
void createPollVote(
@Header("Authorization") String bearerToken,
@Field("id_hash") String idHash,
@Field("poll_options") ArrayList<Integer> pollOptions,
Callback<Response> callback);
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import com.pennapps.labs.pennmobile.api.classes.AccessTokenResponse
import com.pennapps.labs.pennmobile.dining.classes.DiningPreferences
import com.pennapps.labs.pennmobile.dining.classes.DiningRequest
import com.pennapps.labs.pennmobile.fitness.classes.FitnessPreferences
import com.pennapps.labs.pennmobile.fitness.classes.FitnessRequest
import com.pennapps.labs.pennmobile.fitness.classes.FitnessRoom
import com.pennapps.labs.pennmobile.fitness.classes.FitnessRoomUsage
import com.pennapps.labs.pennmobile.gsr.classes.GSR
import com.pennapps.labs.pennmobile.gsr.classes.GSRBookingResult
import com.pennapps.labs.pennmobile.gsr.classes.WhartonStatus
import com.pennapps.labs.pennmobile.home.classes.Article
import com.pennapps.labs.pennmobile.home.classes.CalendarEvent
import com.pennapps.labs.pennmobile.home.classes.Poll
import com.pennapps.labs.pennmobile.laundry.classes.LaundryPreferences
import com.pennapps.labs.pennmobile.laundry.classes.LaundryRequest
import com.pennapps.labs.pennmobile.laundry.classes.LaundryRoom
import com.pennapps.labs.pennmobile.laundry.classes.LaundryRoomSimple
import com.pennapps.labs.pennmobile.laundry.classes.LaundryUsage
import okhttp3.ResponseBody
import retrofit.Callback
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Headers
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query
Expand Down Expand Up @@ -56,7 +61,7 @@ interface StudentLifeRf2 {
@GET("laundry/hall/{id}")
fun roomObservable(
@Path("id") id: Int
): Observable<LaundryRoom?>?
): Observable<LaundryRoom?>

@GET("laundry/usage/{id}")
suspend fun usage(
Expand All @@ -75,19 +80,19 @@ interface StudentLifeRf2 {
): Response<ResponseBody>

@GET("penndata/fitness/rooms/")
fun getFitnessRooms(): Observable<List<FitnessRoom?>?>?
fun getFitnessRooms(): Observable<List<FitnessRoom?>?>

@GET("penndata/fitness/usage/{id}")
fun getFitnessRoomUsage(
@Path("id") id: Int,
@Query("num_samples") samples: Int,
@Query("group_by") groupBy: String?
): Observable<FitnessRoomUsage?>?
): Observable<FitnessRoomUsage?>

@GET("penndata/fitness/preferences")
fun getFitnessPreferences(
@Header("Authorization") bearerToken: String?
): Observable<FitnessPreferences?>?
): Observable<FitnessPreferences?>

@POST("penndata/fitness/preferences/")
suspend fun sendFitnessPref(
Expand All @@ -98,25 +103,67 @@ interface StudentLifeRf2 {
@GET("dining/preferences")
fun getDiningPreferences(
@Header("Authorization") bearerToken: String?
): Observable<DiningPreferences?>?
): Observable<DiningPreferences?>

@GET("gsr/availability/{id}/{gid}")
fun gsrRoom(
@Header("Authorization") bearerToken: String?,
@Path("id") id: String?,
@Path("gid") gid: Int,
@Query("start") date: String?
): Observable<GSR?>?
): Observable<GSR?>

@GET("gsr/wharton")
fun isWharton(
@Header("Authorization") bearerToken: String?
): Observable<WhartonStatus?>?
): Observable<WhartonStatus?>

@GET("penndata/news")
fun getNews(): Observable<Article?>?
fun getNews(): Observable<Article?>

@GET("penndata/calendar")
fun getCalendar(): Observable<List<CalendarEvent?>?>?
fun getCalendar(): Observable<List<CalendarEvent?>?>

@FormUrlEncoded
@POST("gsr/book/")
suspend fun bookGSR(
@Header("Authorization") bearerToken: String,
@Field("start_time") start: String?,
@Field("end_time") end: String?,
@Field("gid") gid: Int,
@Field("id") id: Int,
@Field("room_name") roomName: String,
): Response<GSRBookingResult>

@FormUrlEncoded
@POST("gsr/cancel/")
suspend fun cancelReservation(
@Header("Authorization") bearerToken: String,
@Header("X-Device-ID") deviceID: String?,
@Field("booking_id") bookingID: String?,
@Field("sessionid") sessionID: String?,
): Response<ResponseBody>

@Headers("Content-Type: application/json")
@POST("dining/preferences/")
suspend fun sendDiningPref(
@Header("Authorization") bearerToken: String,
@Body body: DiningRequest,
): Response<ResponseBody>

@FormUrlEncoded
@POST("portal/polls/browse/")
fun browsePolls(
@Header("Authorization") bearerToken: String,
@Field("id_hash") idHash: String
): Observable<List<Poll?>?>

@FormUrlEncoded
@POST("/portal/votes/")
suspend fun createPollVote(
@Header("Authorization") bearerToken: String,
@Field("id_hash") idHash: String,
@Field("poll_options") pollOptions: ArrayList<Int>,
): Response<ResponseBody>
}

Loading

0 comments on commit 581955b

Please sign in to comment.