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

Added nav modes enum and user's identifiers #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions app/src/main/java/com/baato/osmnavigationapp/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.baato.osmnavigationapp;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -16,29 +17,41 @@
import com.baato.baatolibrary.services.BaatoRouting;
import com.baato.baatolibrary.services.BaatoReverse;
import com.baato.baatolibrary.services.BaatoSearch;
import com.baato.baatolibrary.utilities.BaatoNavMode;
import com.baato.baatolibrary.utilities.BaatoUtil;
import com.kathmandulivinglabs.osmnavigationapp.R;

import java.util.UUID;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "apple";
private static final String TAG = "Baato-JAVA-CLIENT";
private String sessionId;
private String bundleIdentifier;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sessionId = UUID.randomUUID().toString();
bundleIdentifier = MainActivity.this.getPackageName();

performRouting();
performReverseGeoCoding();
performSearch();
getPlaces();

}

private void performRouting() {
String points[] = new String[]{"27.73405,85.33685", "27.7177,85.3278"};
new BaatoRouting(this)
.setPoints(points)
.setAccessToken(Constants.TOKEN)
.setMode("foot")
.setSessionId(sessionId)
.setBundleIdentifier(bundleIdentifier)
.setMode(BaatoNavMode.BIKE)
.setAlternatives(false)
.setInstructions(false)
.withListener(new BaatoRouting.BaatoRoutingRequestListener() {
Expand All @@ -62,6 +75,8 @@ private void performReverseGeoCoding() {
.setLatLon(new LatLon(27.73405, 85.33685))
.setAPIVersion("1")
.setAccessToken(Constants.TOKEN)
.setSessionId(sessionId)
.setBundleIdentifier(bundleIdentifier)
.setLimit(5)
.withListener(new BaatoReverse.BaatoReverseRequestListener() {
@Override
Expand All @@ -83,6 +98,8 @@ private void getPlaces() {
new BaatoPlace(this)
.setAccessToken(Constants.TOKEN)
.setPlaceId(101499)
.setSessionId(sessionId)
.setBundleIdentifier(bundleIdentifier)
.withListener(new BaatoPlace.BaatoPlaceListener() {
@Override
public void onSuccess(PlaceAPIResponse place) {
Expand All @@ -104,6 +121,8 @@ private void performSearch() {
.setAccessToken(Constants.TOKEN)
.setAPIVersion("1")
.setQuery("Kathmandu")
.setSessionId(sessionId)
.setBundleIdentifier(bundleIdentifier)
.setLimit(5)
.withListener(new BaatoSearch.BaatoSearchRequestListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.baato.baatolibrary.utilities.LoggingInterceptor;
import com.baato.baatolibrary.utilities.RetryInterceptor;

import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -38,6 +39,7 @@ public static Retrofit retrofitV2(String apiVersion, String apiBaseURL) {
.readTimeout(15, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.addInterceptor(new RetryInterceptor(4))
// .addInterceptor(new LoggingInterceptor())
.build();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(apiBaseURL+ "v" + apiVersion + "/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,35 @@

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.HeaderMap;
import retrofit2.http.Headers;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;

public interface BaatoAPI {
@GET("directions")
Call<DirectionsAPIResponse> getDirections(@QueryMap Map<String, Object> filters, @Query("points[]") String[] points);
Call<DirectionsAPIResponse> getDirections(
@Header("SessionId") String sessionId,
@Header("BundleIdentifier") String bundleIdentifier,
@QueryMap Map<String, Object> filters, @Query("points[]") String[] points);

@GET("search")
Call<SearchAPIResponse> searchQuery(@QueryMap Map<String, String> filters);
Call<SearchAPIResponse> searchQuery(
@Header("SessionId") String sessionId,
@Header("BundleIdentifier") String bundleIdentifier,
@QueryMap Map<String, String> filters);

@GET("reverse")
Call<PlaceAPIResponse> performReverseGeoCode(@QueryMap Map<String, String> filter);
Call<PlaceAPIResponse> performReverseGeoCode(
@Header("SessionId") String sessionId,
@Header("BundleIdentifier") String bundleIdentifier,
@QueryMap Map<String, String> filter);

@GET("places")
Call<PlaceAPIResponse> performPlacesQuery(@QueryMap Map<String, String> filter);
Call<PlaceAPIResponse> performPlacesQuery(
@Header("SessionId") String sessionId,
@Header("BundleIdentifier") String bundleIdentifier,
@QueryMap Map<String, String> filter);

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class BaatoPlace {

private Context context;
private String accessToken, securityCode;
private String accessToken, securityCode,bundleIdentifier,sessionId;
private String apiVersion = "1";
private String apiBaseUrl = "https://api.baato.io/api/";
private BaatoPlaceListener baatoPlaceListener;
Expand Down Expand Up @@ -86,7 +86,20 @@ public BaatoPlace setSecurityCode(String securityCode) {
this.securityCode = securityCode;
return this;
}

/**
* Set the package name.
*/
public BaatoPlace setBundleIdentifier(String bundleIdentifier) {
this.bundleIdentifier = bundleIdentifier;
return this;
}
/**
* Set the session Id.
*/
public BaatoPlace setSessionId(String sessionId) {
this.sessionId = sessionId;
return this;
}
/**
* Method to set the UpdateListener for the AppUpdaterUtils actions
*
Expand All @@ -100,7 +113,7 @@ public BaatoPlace withListener(BaatoPlaceListener baatoPlaceListener) {

public void doRequest() {
BaatoAPI baatoAPI = BaatoLib.retrofitV2(apiVersion, apiBaseUrl).create(BaatoAPI.class);
placeAPIResponseCall = baatoAPI.performPlacesQuery(giveMeQueryFilter(context));
placeAPIResponseCall = baatoAPI.performPlacesQuery(sessionId,bundleIdentifier,giveMeQueryFilter(context));
placeAPIResponseCall.enqueue(new Callback<PlaceAPIResponse>() {
@Override
public void onResponse(Call<PlaceAPIResponse> call, Response<PlaceAPIResponse> response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class BaatoReverse {
private static final String TAG = "BaatoReverseGeoCode";
private Context context;
private BaatoReverseRequestListener baatoReverseRequestListener;
private String accessToken, securityCode;
private String accessToken, securityCode,bundleIdentifier,sessionId;
private String apiVersion = "1";
private String apiBaseUrl = "https://api.baato.io/api/";
private int radius, limit = 0;
Expand Down Expand Up @@ -104,6 +104,20 @@ public BaatoReverse setSecurityCode(String securityCode) {
this.securityCode = securityCode;
return this;
}
/**
* Set the package name.
*/
public BaatoReverse setBundleIdentifier(String bundleIdentifier) {
this.bundleIdentifier = bundleIdentifier;
return this;
}
/**
* Set the session Id.
*/
public BaatoReverse setSessionId(String sessionId) {
this.sessionId = sessionId;
return this;
}

/**
* Method to set the UpdateListener for the AppUpdaterUtils actions
Expand All @@ -118,7 +132,7 @@ public BaatoReverse withListener(BaatoReverseRequestListener baatoReverseRequest

public void doRequest() {
BaatoAPI baatoAPI = BaatoLib.retrofitV2(apiVersion, apiBaseUrl).create(BaatoAPI.class);
placeAPIResponseCall = baatoAPI.performReverseGeoCode(giveMeQueryFilter(context));
placeAPIResponseCall = baatoAPI.performReverseGeoCode(sessionId,bundleIdentifier,giveMeQueryFilter(context));
placeAPIResponseCall.enqueue(new Callback<PlaceAPIResponse>() {
@Override
public void onResponse(Call<PlaceAPIResponse> call, Response<PlaceAPIResponse> response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.baato.baatolibrary.models.NavResponse;
import com.baato.baatolibrary.navigation.NavigateResponseConverter;
import com.baato.baatolibrary.requests.BaatoAPI;
import com.baato.baatolibrary.utilities.BaatoNavMode;
import com.baato.baatolibrary.utilities.BaatoUtil;
import com.baato.baatolibrary.utilities.ErrorUtils;

Expand All @@ -26,7 +27,8 @@
public class BaatoRouting {
private Context context;
private BaatoRoutingRequestListener baatoRoutingRequestListener;
private String accessToken, securityCode, mode;
private String accessToken, securityCode,bundleIdentifier,sessionId,mode;
// private BaatoNavMode mode;
private String apiVersion = "1";
private String apiBaseUrl = "https://api.baato.io/api/";
private String[] points;
Expand All @@ -35,7 +37,6 @@ public class BaatoRouting {
private Call<DirectionsAPIResponse> directionsAPIResponseCall;
private Locale locale;


public interface BaatoRoutingRequestListener {
/**
* onSuccess method called after it is successful
Expand All @@ -57,6 +58,20 @@ public BaatoRouting setAccessToken(@NonNull String accessToken) {
this.accessToken = accessToken;
return this;
}
/**
* Set the package name.
*/
public BaatoRouting setBundleIdentifier(String bundleIdentifier) {
this.bundleIdentifier = bundleIdentifier;
return this;
}
/**
* Set the session Id.
*/
public BaatoRouting setSessionId(String sessionId) {
this.sessionId = sessionId;
return this;
}

/**
* Set the apiVersion. By default it takes version "1"
Expand Down Expand Up @@ -88,8 +103,8 @@ public BaatoRouting setAPIBaseURL(@NonNull String apiBaseURL) {
/**
* Set the mode.
*/
public BaatoRouting setMode(@NonNull String mode) {
this.mode = mode;
public BaatoRouting setMode(@NonNull BaatoNavMode mode) {
this.mode = mode.toString().toLowerCase();
return this;
}

Expand Down Expand Up @@ -139,7 +154,7 @@ public BaatoRouting withListener(BaatoRoutingRequestListener baatoRoutingRequest

public void doRequest() {
BaatoAPI baatoAPI = BaatoLib.retrofitV2(apiVersion, apiBaseUrl).create(BaatoAPI.class);
directionsAPIResponseCall = baatoAPI.getDirections(giveMeQueryFilter(context), points);
directionsAPIResponseCall = baatoAPI.getDirections(sessionId,bundleIdentifier,giveMeQueryFilter(context), points);
directionsAPIResponseCall
.enqueue(new Callback<DirectionsAPIResponse>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class BaatoSearch {
private Context context;
private BaatoSearchRequestListener baatoSearchRequestListener;
private String accessToken, query;
private String accessToken, query,bundleIdentifier,sessionId;
private String type, securityCode;
private String apiVersion = "1";
private String apiBaseUrl = "https://api.baato.io/api/";
Expand Down Expand Up @@ -52,6 +52,20 @@ public BaatoSearch setAccessToken(@NonNull String accessToken) {
this.accessToken = accessToken;
return this;
}
/**
* Set the package name.
*/
public BaatoSearch setBundleIdentifier(String bundleIdentifier) {
this.bundleIdentifier = bundleIdentifier;
return this;
}
/**
* Set the session Id.
*/
public BaatoSearch setSessionId(String sessionId) {
this.sessionId = sessionId;
return this;
}

/**
* Set the securityCode is security enabled.
Expand Down Expand Up @@ -133,7 +147,7 @@ public BaatoSearch withListener(BaatoSearchRequestListener baatoSearchRequestLis

public void doRequest() {
BaatoAPI baatoAPI = BaatoLib.retrofitV2(apiVersion, apiBaseUrl).create(BaatoAPI.class);
searchAPIResponseCall = baatoAPI.searchQuery(giveMeQueryFilter(context));
searchAPIResponseCall = baatoAPI.searchQuery(sessionId,bundleIdentifier,giveMeQueryFilter(context));
searchAPIResponseCall.enqueue(new Callback<SearchAPIResponse>() {
@Override
public void onResponse(Call<SearchAPIResponse> call, Response<SearchAPIResponse> response) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baato.baatolibrary.utilities;

public enum BaatoNavMode {
CAR,BIKE,FOOT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.baato.baatolibrary.utilities;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;

public class LoggingInterceptor implements Interceptor {

@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();

// Log request headers
System.out.println("Request Headers: " + originalRequest.headers().get("SessionId"));
System.out.println("Request Headers: " + originalRequest.headers().get("BundleIdentifier"));

// Log request body if present
if (originalRequest.body() != null) {
Buffer buffer = new Buffer();
originalRequest.body().writeTo(buffer);
System.out.println("Request Body: " + buffer.readUtf8());
}

Response response = chain.proceed(originalRequest);

// Log response body if present
ResponseBody responseBody = response.body();
if (responseBody != null) {
BufferedSource source = responseBody.source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();
System.out.println("Response Body: " + buffer.clone().readUtf8());
}

return response;
}
}