-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from LociStar/Weather2.0
Weather2.0
- Loading branch information
Showing
23 changed files
with
795 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,5 @@ build/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
/src/main/resources/application.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
WebAPI/src/main/java/com/locibot/webapi/weatherForecast/WeatherForecast.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.locibot.webapi.weatherForecast; | ||
|
||
import com.github.prominence.openweathermap.api.model.forecast.Forecast; | ||
import com.github.prominence.openweathermap.api.model.onecall.current.CurrentWeatherData; | ||
|
||
public class WeatherForecast { | ||
private final Forecast data; | ||
|
||
public WeatherForecast(Forecast data) { | ||
this.data = data; | ||
} | ||
|
||
public Forecast getData() { | ||
return data; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
WebAPI/src/main/java/com/locibot/webapi/weatherForecast/WeatherForecastError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.locibot.webapi.weatherForecast; | ||
|
||
public class WeatherForecastError { | ||
private final String error; | ||
|
||
public WeatherForecastError(String errorType) { | ||
this.error = errorType; | ||
} | ||
|
||
public String getError() { | ||
return error; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
WebAPI/src/main/java/com/locibot/webapi/weatherForecast/WeatherForecastHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.locibot.webapi.weatherForecast; | ||
|
||
import com.locibot.locibot.data.credential.Credential; | ||
import com.locibot.locibot.data.credential.CredentialManager; | ||
import com.locibot.locibot.utils.JWT.TokenVerification; | ||
import com.locibot.locibot.utils.weather.WeatherManager; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.BodyInserters; | ||
import org.springframework.web.reactive.function.server.ServerRequest; | ||
import org.springframework.web.reactive.function.server.ServerResponse; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.Objects; | ||
|
||
@Component | ||
public class WeatherForecastHandler { | ||
public Mono<ServerResponse> weatherForecast(ServerRequest request) { | ||
|
||
String token = request.queryParam("token").orElse("invalid login token"); | ||
String city = request.queryParam("city").orElse("XXX_No_City"); | ||
Double longitude = Double.parseDouble(request.queryParam("long").orElse("0")); | ||
Double latitude = Double.parseDouble(request.queryParam("lat").orElse("0")); | ||
WeatherManager weatherManager = new WeatherManager(); | ||
|
||
TokenVerification tokenVerification = new TokenVerification(token); | ||
|
||
try { | ||
if (tokenVerification.verify(Objects.requireNonNull(CredentialManager.get(Credential.JWT_SECRET)))) { | ||
return weatherManager.getSaved5DayWeatherData(city, longitude, latitude).flatMap(data -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON) | ||
.body(BodyInserters.fromValue(data))); | ||
} | ||
} catch (Exception e) { | ||
return ServerResponse.badRequest().contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(new WeatherForecastError("bad token"))); | ||
} | ||
|
||
return ServerResponse.badRequest().contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(new WeatherForecastError("fatal error"))); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
WebAPI/src/main/java/com/locibot/webapi/weatherForecast/WeatherForecastRouter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.locibot.webapi.weatherForecast; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.reactive.function.server.RouterFunction; | ||
import org.springframework.web.reactive.function.server.RouterFunctions; | ||
import org.springframework.web.reactive.function.server.ServerResponse; | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
public class WeatherForecastRouter { | ||
@Bean | ||
public RouterFunction<ServerResponse> weatherForecastRoute(WeatherForecastHandler weatherForecastHandler) { | ||
|
||
return RouterFunctions.route() | ||
.GET("/weatherForecast", weatherForecastHandler::weatherForecast) //, RequestPredicates.queryParam("token", t -> true) | ||
.build(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.