Skip to content

Commit

Permalink
update: use Gson modified amap
Browse files Browse the repository at this point in the history
Signed-off-by: PolarishT <[email protected]>
  • Loading branch information
PolarishT committed Dec 17, 2024
1 parent bcdce52 commit 3a621cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

package com.alibaba.cloud.ai.functioncalling.amp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.function.Function;

Expand All @@ -31,42 +31,42 @@
*/
public class WeatherSearchService implements Function<WeatherSearchService.Request, WeatherSearchService.Response> {

private final WeatherTools weatherTools;
private final WeatherTools weatherTools;

public WeatherSearchService(AmapProperties amapProperties) {
this.weatherTools = new WeatherTools(amapProperties);
}
public WeatherSearchService (AmapProperties amapProperties) {
this.weatherTools = new WeatherTools(amapProperties);
}

@Override
public Response apply(Request request) {
@Override
public Response apply (Request request) {

String responseBody = weatherTools.getAddressCityCode(request.address);
String responseBody = weatherTools.getAddressCityCode(request.address);

String adcode = "";
String adcode = "";

try {
JSONObject jsonObject = JSON.parseObject(responseBody);
JSONArray geocodesArray = jsonObject.getJSONArray("geocodes");
if (geocodesArray != null && !geocodesArray.isEmpty()) {
JSONObject firstGeocode = geocodesArray.getJSONObject(0);
adcode = firstGeocode.getString("adcode");
}
}
catch (Exception e) {
return new Response("Error occurred while processing the request.");
}
try {
JsonObject jsonObject = JsonParser.parseString(responseBody).getAsJsonObject();
JsonArray geocodesArray = jsonObject.getAsJsonArray("geocodes");
if (geocodesArray != null && !geocodesArray.isEmpty()) {
JsonObject firstGeocode = geocodesArray.get(0).getAsJsonObject();
adcode = firstGeocode.get("adcode").getAsString();
}
}
catch (Exception e) {
return new Response("Error occurred while processing the request.");
}

String weather = weatherTools.getWeather(adcode);
String weather = weatherTools.getWeather(adcode);

return new Response(weather);
}
return new Response(weather);
}

@JsonClassDescription("Get the weather conditions for a specified address.")
public record Request(
@JsonProperty(required = true, value = "address") @JsonPropertyDescription("The address") String address) {
}
@JsonClassDescription("Get the weather conditions for a specified address.")
public record Request(
@JsonProperty(required = true, value = "address") @JsonPropertyDescription("The address") String address) {
}

public record Response(String message) {
}
public record Response(String message) {
}

}

0 comments on commit 3a621cd

Please sign in to comment.