Skip to content

Commit

Permalink
refactor: reformatting code
Browse files Browse the repository at this point in the history
  • Loading branch information
bazrafkan committed Jan 21, 2022
1 parent 6889d2c commit fc392cf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
7 changes: 5 additions & 2 deletions lib/src/photos/photos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class Photos {
maxHeight,
maxWidth,
);
var uri = NetworkUtility.createUri(proxyUrl, _authority, _unencodedPath, queryParameters);
var uri = NetworkUtility.createUri(
proxyUrl, _authority, _unencodedPath, queryParameters);
var response = await NetworkUtility.fetchUrl(uri, headers: headers);
if (response != null) {
List<int> list = response.codeUnits;
Expand Down Expand Up @@ -80,7 +81,9 @@ class Photos {
);
var uri = Uri.https(
proxyUrl != null && proxyUrl != '' ? proxyUrl! : _authority,
proxyUrl != null && proxyUrl != '' ? 'https://$_authority/$_unencodedPath' : _unencodedPath,
proxyUrl != null && proxyUrl != ''
? 'https://$_authority/$_unencodedPath'
: _unencodedPath,
queryParameters,
);
return await NetworkUtility.fetchUrl(uri, headers: headers);
Expand Down
6 changes: 4 additions & 2 deletions lib/src/query_autocomplete/query_autocomplete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class QueryAutocomplete {
radius,
language,
);
var uri = NetworkUtility.createUri(proxyUrl, _authority, _unencodedPath, queryParameters);
var uri = NetworkUtility.createUri(
proxyUrl, _authority, _unencodedPath, queryParameters);
var response = await NetworkUtility.fetchUrl(uri, headers: headers);
if (response != null) {
return AutocompleteResponse.parseAutocompleteResult(response);
Expand Down Expand Up @@ -95,7 +96,8 @@ class QueryAutocomplete {
language,
);

var uri = NetworkUtility.createUri(proxyUrl, _authority, _unencodedPath, queryParameters);
var uri = NetworkUtility.createUri(
proxyUrl, _authority, _unencodedPath, queryParameters);
return await NetworkUtility.fetchUrl(uri, headers: headers);
}

Expand Down
15 changes: 10 additions & 5 deletions lib/src/search/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import 'package:google_place/src/utils/network_utility.dart';

class Search {
static final _authority = 'maps.googleapis.com';
static final _unencodedPathFindPlace = 'maps/api/place/findplacefromtext/json';
static final _unencodedPathFindPlace =
'maps/api/place/findplacefromtext/json';
static final _unencodedPathNearBySearch = 'maps/api/place/nearbysearch/json';
static final _unencodedPathTextSearch = 'maps/api/place/textsearch/json';
final String apiKEY;
Expand Down Expand Up @@ -49,7 +50,8 @@ class Search {
locationbias,
);

var uri = NetworkUtility.createUri(proxyUrl, _authority, _unencodedPathFindPlace, queryParameters);
var uri = NetworkUtility.createUri(
proxyUrl, _authority, _unencodedPathFindPlace, queryParameters);
var response = await NetworkUtility.fetchUrl(uri, headers: headers);
if (response != null) {
return FindPlaceResponse.parseFindPlaceResult(response);
Expand Down Expand Up @@ -124,7 +126,8 @@ class Search {
pagetoken,
);

var uri = Uri.https(_authority, _unencodedPathNearBySearch, queryParameters);
var uri =
Uri.https(_authority, _unencodedPathNearBySearch, queryParameters);
var response = await NetworkUtility.fetchUrl(uri, headers: headers);
if (response != null) {
return NearBySearchResponse.parseNearBySearchResult(response);
Expand Down Expand Up @@ -326,7 +329,8 @@ class Search {
pagetoken,
);

var uri = Uri.https(_authority, _unencodedPathNearBySearch, queryParameters);
var uri =
Uri.https(_authority, _unencodedPathNearBySearch, queryParameters);
return await NetworkUtility.fetchUrl(uri, headers: headers);
}

Expand Down Expand Up @@ -447,7 +451,8 @@ class Search {
value = 'ipbias';
}
if (locationbias.point != null) {
value = 'point:${locationbias.point!.latitude},${locationbias.point!.longitude}';
value =
'point:${locationbias.point!.latitude},${locationbias.point!.longitude}';
}
if (locationbias.circular != null) {
value =
Expand Down
19 changes: 12 additions & 7 deletions lib/src/utils/network_utility.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class NetworkUtility {
Map<String, String>? headers,
}) async {
try {
final response = await http.get(uri, headers: headers).timeout(GooglePlace.timeout);
final response =
await http.get(uri, headers: headers).timeout(GooglePlace.timeout);
if (response.statusCode == 200) {
return response.body;
}
Expand All @@ -23,8 +24,8 @@ class NetworkUtility {
/// [authority] Required parameters - the domain name of the google server, usually https://maps.googleapis.com
/// [unencodedGoogleMapsPath] Required parameters - the path to the api, usually something like maps/api/...
/// [queryParameters] Required parameters - a map of query parameters to be appended to the url
static Uri createUri(String? proxyUrl, String authority, String unencodedGoogleMapsPath,
Map<String, String?> queryParameters) {
static Uri createUri(String? proxyUrl, String authority,
String unencodedGoogleMapsPath, Map<String, String?> queryParameters) {
Uri uri;
final googleApiUri = Uri.https(
authority,
Expand All @@ -45,14 +46,18 @@ class NetworkUtility {
}

if (proxyHostname.contains("/")) {
everythingAfterHostname = proxyHostname.substring(proxyHostname.indexOf("/"));
everythingAfterHostname =
proxyHostname.substring(proxyHostname.indexOf("/"));
proxyHostname = proxyHostname.substring(0, proxyHostname.indexOf("/"));
}

if (everythingAfterHostname.contains("?") && everythingAfterHostname.contains("=")) {
var proxyPath = everythingAfterHostname.substring(0, everythingAfterHostname.indexOf("?"));
if (everythingAfterHostname.contains("?") &&
everythingAfterHostname.contains("=")) {
var proxyPath = everythingAfterHostname.substring(
0, everythingAfterHostname.indexOf("?"));
var parameterName = everythingAfterHostname.substring(
everythingAfterHostname.indexOf("?") + 1, everythingAfterHostname.indexOf("="));
everythingAfterHostname.indexOf("?") + 1,
everythingAfterHostname.indexOf("="));
var googleMapsUrlParam = {parameterName: googleApiUri.toString()};
queryParameters.addAll(googleMapsUrlParam);
if (usingHttps) {
Expand Down
9 changes: 6 additions & 3 deletions test/google_place_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'package:google_place/google_place.dart';
void main() {
String apiKey = "Your-Key";
test('init', () async {
var googlePlace = GooglePlace(apiKey, proxyUrl: 'cors-anywhere.herokuapp.com');
var googlePlace =
GooglePlace(apiKey, proxyUrl: 'cors-anywhere.herokuapp.com');
expect(googlePlace.apiKEY, apiKey);
expect(googlePlace.search.apiKEY, apiKey);
expect(googlePlace.details.apiKEY, apiKey);
Expand All @@ -22,9 +23,11 @@ void main() {
await googlePlace.photos.get("photoReference", 10, 10);
googlePlace = GooglePlace(apiKey, proxyUrl: 'https://1.2.3.4/proxy');
await googlePlace.queryAutocomplete.get("some place");
googlePlace = GooglePlace(apiKey, proxyUrl: 'https://localhost:6969/proxy/proxy-two/');
googlePlace = GooglePlace(apiKey,
proxyUrl: 'https://localhost:6969/proxy/proxy-two/');
await googlePlace.search.getFindPlace("some place", InputType.TextQuery);
googlePlace = GooglePlace(apiKey, proxyUrl: 'localhost:6969/proxy/proxy-two?gmapsurl=');
googlePlace = GooglePlace(apiKey,
proxyUrl: 'localhost:6969/proxy/proxy-two?gmapsurl=');
await googlePlace.autocomplete.get("some place");
//try a bad hostname
googlePlace = GooglePlace(apiKey, proxyUrl: 'localhost:6969******');
Expand Down

0 comments on commit fc392cf

Please sign in to comment.