Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add sidecar base url #1281

Merged
merged 8 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions onebusaway-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ android {
buildConfigField "String", "FIXED_REGION_PAYMENT_WARNING_BODY", "null"
buildConfigField "boolean", "FIXED_REGION_TRAVEL_BEHAVIOR_DATA_COLLECTION", "false"
buildConfigField "boolean", "FIXED_REGION_ENROLL_PARTICIPANTS_IN_STUDY", "false"
buildConfigField "String", "FIXED_REGION_SIDECAR_BASE_URL", "\"https://onebusaway.co\""
buildConfigField "String", "ONESIGNAL_APP_ID", ONESIGNAL_APP_ID
}

Expand Down Expand Up @@ -160,6 +161,7 @@ android {
buildConfigField "String", "FIXED_REGION_PAYMENT_WARNING_BODY", "null"
buildConfigField "boolean", "FIXED_REGION_TRAVEL_BEHAVIOR_DATA_COLLECTION", "false"
buildConfigField "boolean", "FIXED_REGION_ENROLL_PARTICIPANTS_IN_STUDY", "false"
buildConfigField "String", "FIXED_REGION_SIDECAR_BASE_URL", "null"
buildConfigField "String", "ONESIGNAL_APP_ID", ONESIGNAL_APP_ID
}

Expand Down Expand Up @@ -203,6 +205,7 @@ android {
buildConfigField "String", "FIXED_REGION_PAYMENT_WARNING_BODY", "null"
buildConfigField "boolean", "FIXED_REGION_TRAVEL_BEHAVIOR_DATA_COLLECTION", "false"
buildConfigField "boolean", "FIXED_REGION_ENROLL_PARTICIPANTS_IN_STUDY", "false"
buildConfigField "String", "FIXED_REGION_SIDECAR_BASE_URL", "null"
buildConfigField "String", "ONESIGNAL_APP_ID", ONESIGNAL_APP_ID
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public static ObaRegion getRegionWithPathNoSeparator(Context context) {
null,
null,
false,
false);
false,
"https://onebusaway.co");
}

/**
Expand Down Expand Up @@ -135,7 +136,8 @@ public static ObaRegion getRegionNoSeparator(Context context) {
null,
null,
false,
false);
false,
"https://onebusaway.co");
}

/**
Expand Down Expand Up @@ -175,7 +177,8 @@ public static ObaRegion getRegionWithPort(Context context) {
null,
null,
false,
false);
false,
"https://onebusaway.co");
}

/**
Expand Down Expand Up @@ -215,7 +218,8 @@ public static ObaRegion getRegionNoScheme(Context context) {
null,
null,
false,
false);
false,
"https://onebusaway.co");
}

/**
Expand Down Expand Up @@ -255,7 +259,8 @@ public static ObaRegion getRegionWithHttps() {
null,
null,
false,
false);
false,
"https://onebusaway.co");
}

/**
Expand Down Expand Up @@ -295,7 +300,8 @@ public static ObaRegion getRegionWithHttpsAndPort() {
null,
null,
false,
false);
false,
"https://onebusaway.co");
}

/**
Expand Down Expand Up @@ -333,7 +339,8 @@ public static ObaRegion getRegionWithoutObaApis(Context context) {
null,
null,
false,
false);
false,
"https://onebusaway.co");
}

/**
Expand Down Expand Up @@ -371,6 +378,7 @@ public static ObaRegion getInactiveRegion(Context context) {
null,
null,
false,
false);
false,
"https://onebusaway.co");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public interface Open311Server {
*/
public String getObaBaseUrl();

/**
* @return The sideCar Vase URL for this region, or null if it doesn't have a sideCar URL.
*/

public String getSidecarBaseUrl();

/**
* @return The base SIRI URL for this region, or null if it doesn't use SIRI.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public String toString() {

private final String obaBaseUrl;

private final String sidecarBaseUrl;

private final String siriBaseUrl;

private final Bounds[] bounds;
Expand Down Expand Up @@ -210,6 +212,7 @@ public String toString() {
paymentWarningBody = null;
travelBehaviorDataCollectionEnabled = false;
enrollParticipantsInStudy = false;
sidecarBaseUrl = "";
}

public ObaRegionElement(long id,
Expand All @@ -235,7 +238,8 @@ public ObaRegionElement(long id,
String paymentWarningTitle,
String paymentWarningBody,
boolean travelBehaviorDataCollectionEnabled,
boolean enrollParticipantsInStudy) {
boolean enrollParticipantsInStudy,
String sidecarBaseUrl) {
this.id = id;
this.regionName = name;
this.active = active;
Expand All @@ -260,6 +264,7 @@ public ObaRegionElement(long id,
this.paymentWarningBody = paymentWarningBody;
this.travelBehaviorDataCollectionEnabled = travelBehaviorDataCollectionEnabled;
this.enrollParticipantsInStudy = enrollParticipantsInStudy;
this.sidecarBaseUrl = sidecarBaseUrl;
}

@Override
Expand All @@ -282,6 +287,11 @@ public String getObaBaseUrl() {
return obaBaseUrl;
}

@Override
public String getSidecarBaseUrl() {
return sidecarBaseUrl;
}

@Override
public String getSiriBaseUrl() {
return siriBaseUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static class Builder extends PostBuilderBase {

public Builder(Context context) {
super(context, null);
String baseUrl = context.getString(R.string.arrivals_reminders_api_url);
String baseUrl = context.getString(R.string.arrivals_reminders_api_endpoint);
Application app = Application.get();
uriBuilder = Uri.parse(baseUrl).buildUpon();
buildAPIURL(String.valueOf(app.getCurrentRegion().getId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public static class Builder {
private static Uri URI = null;

public Builder(Context context) {
String studyAPIURL = Application.get().getResources().getString(R.string.studies_api_url);
String baseUrl = Application.get().getCurrentRegion().getSidecarBaseUrl();
if(baseUrl == null) return;
String studyAPIURL = baseUrl + Application.get().getResources().getString(R.string.studies_api_endpoint);
studyAPIURL = studyAPIURL.replace("regionID", String.valueOf(Application.get().getCurrentRegion().getId()));
URI = Uri.parse(studyAPIURL).buildUpon().appendQueryParameter("user_id", SurveyPreferences.getUserUUID(context)).build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.onebusaway.android.io.request.weather;

import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;

Expand All @@ -23,10 +24,12 @@ public static class Builder {
private static Uri URI = null;

public Builder(long regionId) {
String weatherAPIURL = Application.get().getResources().getString(R.string.weather_api_url);

String baseUrl = Application.get().getCurrentRegion().getSidecarBaseUrl();
if(baseUrl == null) return;
String weatherAPIURL = baseUrl + Application.get().getResources().getString(R.string.weather_api_endpoint);
// Replacing param regionID with our current region id.
weatherAPIURL = weatherAPIURL.replace("regionID",String.valueOf(regionId));
Log.e("WeatherAPIURL", weatherAPIURL);
URI = Uri.parse(weatherAPIURL);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,14 @@ protected interface RegionsColumns {
*/
public static final String OBA_BASE_URL = "oba_base_url";

/**
* The Sidecar Base URL for the API.
* <P>
* Type: TEXT
* </P>
*/
public static final String SIDECAR_BASE_URL = "sidecar_base_url";

/**
* The base SIRI URL.
* <P>
Expand Down Expand Up @@ -1404,7 +1412,8 @@ public static ObaRegion get(ContentResolver cr, int id) {
PAYMENT_WARNING_TITLE,
PAYMENT_WARNING_BODY,
TRAVEL_BEHAVIOR_DATA_COLLECTION,
ENROLL_PARTICIPANTS_IN_STUDY
ENROLL_PARTICIPANTS_IN_STUDY,
SIDECAR_BASE_URL
};

Cursor c = cr.query(buildUri((int) id), PROJECTION, null, null, null);
Expand Down Expand Up @@ -1437,7 +1446,8 @@ public static ObaRegion get(ContentResolver cr, int id) {
c.getString(17), // Payment Warning Title
c.getString(18), // Payment Warning Body
c.getInt(19) > 0, // Travel behavior data collection
c.getInt(20) > 0 // Enroll participants in travel behavior study
c.getInt(20) > 0, // Enroll participants in travel behavior study
c.getString(21) // Sidecar Base URL
);
} finally {
c.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ObaProvider extends ContentProvider {

private class OpenHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 31;
private static final int DATABASE_VERSION = 32;

public OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Expand Down Expand Up @@ -311,6 +311,12 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 30) {
db.execSQL("DROP TABLE IF EXISTS " + ObaContract.Trips.PATH);
createTripsTable(db);
++oldVersion;
}

if(oldVersion == 31) {
db.execSQL("ALTER TABLE " + ObaContract.Regions.PATH +
" ADD COLUMN " + ObaContract.Regions.SIDECAR_BASE_URL + " VARCHAR");
}

}
Expand Down Expand Up @@ -621,6 +627,8 @@ private void dropTables(SQLiteDatabase db) {
.put(ObaContract.RegionOpen311Servers.API_KEY, ObaContract.RegionOpen311Servers.API_KEY);
sRegionOpen311ProjectionMap
.put(ObaContract.RegionOpen311Servers.BASE_URL, ObaContract.RegionOpen311Servers.BASE_URL);
sRegionsProjectionMap
.put(ObaContract.Regions.SIDECAR_BASE_URL, ObaContract.Regions.SIDECAR_BASE_URL);
}

private SQLiteDatabase mDb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ private boolean validateSurveyAnswers(StudyResponse.Surveys survey, boolean hero
* @return apiURl
*/
private String getSubmitSurveyApiUrl(boolean isHeroQuestion) {
String apiUrl = context.getString(R.string.submit_survey_api_url);
String baseUrl = Application.get().getCurrentRegion().getSidecarBaseUrl();
String apiUrl = baseUrl + context.getString(R.string.submit_survey_api_endpoint);
if (!isHeroQuestion && updateSurveyPath != null) {
apiUrl += updateSurveyPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ public static ArrayList<ObaRegion> getRegionsFromProvider(Context context) {
ObaContract.Regions.PAYMENT_WARNING_TITLE,
ObaContract.Regions.PAYMENT_WARNING_BODY,
ObaContract.Regions.TRAVEL_BEHAVIOR_DATA_COLLECTION,
ObaContract.Regions.ENROLL_PARTICIPANTS_IN_STUDY
ObaContract.Regions.ENROLL_PARTICIPANTS_IN_STUDY,
ObaContract.Regions.SIDECAR_BASE_URL
};

ContentResolver cr = context.getContentResolver();
Expand Down Expand Up @@ -469,7 +470,8 @@ public static ArrayList<ObaRegion> getRegionsFromProvider(Context context) {
c.getString(17), // Payment Warning Title
c.getString(18), // Payment Warning Body
c.getInt(19) > 0, // travel behavior data collection enabled for region
c.getInt(20) > 0 // enrolling participants for travel behavior data collection
c.getInt(20) > 0, // enrolling participants for travel behavior data collection
c.getString(21) //Sidecar base URL
));

} while (c.moveToNext());
Expand Down Expand Up @@ -658,7 +660,8 @@ public static ObaRegion getRegionFromBuildFlavor() {
BuildConfig.FIXED_REGION_PAYMENT_WARNING_TITLE,
BuildConfig.FIXED_REGION_PAYMENT_WARNING_BODY,
BuildConfig.FIXED_REGION_TRAVEL_BEHAVIOR_DATA_COLLECTION,
BuildConfig.FIXED_REGION_ENROLL_PARTICIPANTS_IN_STUDY);
BuildConfig.FIXED_REGION_ENROLL_PARTICIPANTS_IN_STUDY,
BuildConfig.FIXED_REGION_SIDECAR_BASE_URL);
return region;
}

Expand Down Expand Up @@ -737,6 +740,7 @@ private static ContentValues toContentValues(ObaRegion region) {
region.isTravelBehaviorDataCollectionEnabled() ? 1 : 0);
values.put(ObaContract.Regions.ENROLL_PARTICIPANTS_IN_STUDY,
region.isEnrollParticipantsInStudy() ? 1 : 0);
values.put(ObaContract.Regions.SIDECAR_BASE_URL, region.getSidecarBaseUrl());
return values;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ public void fetchAlerts(String regionId, GtfsAlertCallBack callback) {
Log.d(TAG, "Alerts already fetched for region: " + regionId);
return;
}
String pathUrl = getGtfsAlertsUrl(regionId);
if (pathUrl == null) {
return;
}
Log.d(TAG, "fetchAlerts for region: " + regionId);
new Thread(() -> {
try {
String pathUrl = getGtfsAlertsUrl(regionId);
URL url = new URL(pathUrl);
GtfsRealtime.FeedMessage feed = GtfsRealtime.FeedMessage.parseFrom(url.openStream());
processAlerts(feed.getEntityList(), callback);
Expand Down Expand Up @@ -87,11 +90,13 @@ public void processAlerts(List<GtfsRealtime.FeedEntity> alerts, GtfsAlertCallBac
public String getGtfsAlertsUrl(String regionId) {
Application app = Application.get();
SharedPreferences sharedPreferences = Application.getPrefs();

String baseUrl = app.getCurrentRegion().getSidecarBaseUrl();
if (baseUrl == null) return null;
boolean isTestAlert = sharedPreferences.getBoolean(app.getString(R.string.preferences_display_test_alerts), false);
String url = "https://onebusaway.co/api/v1/regions/" + regionId + "/alerts.pb";
if (isTestAlert) url += "?test=1";
return url;
String alertAPIURL = baseUrl + app.getString(R.string.alerts_api_endpoint);
alertAPIURL = alertAPIURL.replace("regionID", regionId);
if (isTestAlert) alertAPIURL += "?test=1";
return alertAPIURL;
}

}
11 changes: 7 additions & 4 deletions onebusaway-android/src/main/res/values/donottranslate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,19 @@
<string name="regions_api_url">https://regions.onebusaway.org/regions-v3.json</string>

<!-- Weather API URL -->
<string name="weather_api_url">https://onebusaway.co/api/v1/regions/regionID/weather.json</string>
<string name="weather_api_endpoint">/api/v1/regions/regionID/weather.json</string>

<!-- GET Survey API URL -->
<string name="studies_api_url">https://onebusaway.co/api/v1/regions/regionID/surveys.json</string>
<string name="studies_api_endpoint">/api/v1/regions/regionID/surveys.json</string>

<!-- POST Survey API URL -->
<string name="submit_survey_api_url">https://onebusaway.co/api/v1/survey_responses/</string>
<string name="submit_survey_api_endpoint">/api/v1/survey_responses/</string>

<!-- Arrivals Alarm API URL -->
<string name="arrivals_reminders_api_url">https://onebusaway.co/api/v1/regions/</string>
<string name="arrivals_reminders_api_endpoint">/api/v1/regions/</string>

<!-- Alerts API Endpoint -->
<string name="alerts_api_endpoint">/api/v1/regions/regionID/alerts.pb</string>

<!-- Donate URL -->
<string name="donate_url">https://onebusaway.org/donate/</string>
Expand Down
Loading