第三方接口-苹果-获取天气接口

1.rquest


import lombok.Data;/*** @version 1.0* @description: 天气* @date 2024/9/12 15:18*/
@Data
public class WeatherRequest {/*** 语言*/private String lang;/*** 经度*/private String lat;/*** 纬度*/private String lon;/*** 城市*/private String city;
}

2.Controller

import com.engwe.app.api.request.weather.WeatherRequest;
import com.engwe.app.trigger.controller.http.weather.service.WeatherService;
import com.engwe.common.frame.types.dto.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;@RestController
@RequestMapping("/api/app/weather")
@Validated
public class WeatherController {@Resourceprivate WeatherService weatherService;@PostMapping("/index")public R<Map<String, Object>> index(@RequestBody @Valid WeatherRequest request) {return R.ok(weatherService.weather(request));}
}

3 service

package com.engwe.app.trigger.controller.http.weather.service;import cn.hutool.jwt.JWTUtil;
import cn.hutool.jwt.signers.JWTSigner;
import cn.hutool.jwt.signers.JWTSignerUtil;
import com.engwe.app.api.IWeatherApi;
import com.engwe.app.api.request.weather.WeatherRequest;
import com.engwe.app.trigger.controller.http.weather.properties.WeatherProperties;
import com.engwe.common.redis.utils.RedisUtils;
import com.engwe.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.time.Duration;
import java.util.*;/*** 天气服务** @link https://developer.apple.com/documentation/weatherkitrestapi/get-api-v1-weather-_language_-_latitude_-_longitude_*/
@Slf4j
@Service
public class WeatherService {@Resourceprivate WeatherProperties weatherProperties;@Resourceprivate IWeatherApi weatherApi;public Map<String, Object> weather(WeatherRequest request) {try {String cityWeatherCacheKey = StringUtils.joinWith(":", "weather", request.getCity());Map<String, Object> cacheObject = getWeatherFromCache(cityWeatherCacheKey);if (Objects.nonNull(cacheObject) && !cacheObject.isEmpty()) {return cacheObject;}String token = getToken();Map<String, String> requestParams = new HashMap<>();requestParams.put("countryCode", request.getCity());requestParams.put("dataSets", "currentWeather,forecastHourly");cacheObject = weatherApi.getWeatherByLocation("Bearer " + token, request.getLang(), request.getLat(), request.getLon(), requestParams);RedisUtils.setCacheObject(cityWeatherCacheKey, cacheObject, Duration.ofSeconds(weatherProperties.getTokenTTL() - 60));return cacheObject;} catch (Exception e) {log.error("get weather info error", e);return null;}}private Map<String, Object> getWeatherFromCache(String cityWeatherCacheKey) {Object cacheObject = RedisUtils.getCacheObject(cityWeatherCacheKey);return Objects.nonNull(cacheObject) ? (Map<String, Object>) cacheObject : null;}private String getToken() throws Exception {final String cacheKey = StringUtils.joinWith(":", "weather", "token");Object cacheObject = RedisUtils.getCacheObject(cacheKey);if (Objects.isNull(cacheObject)) {// Define JWT claimslong currentTimeMillis = System.currentTimeMillis();Date now = new Date(currentTimeMillis);Date expiration = new Date(currentTimeMillis + weatherProperties.getTokenTTL() * 1000L); // 1-hour expirationbyte[] keyBytes = Base64.getDecoder().decode(weatherProperties.getKey());PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);KeyFactory keyFactory = KeyFactory.getInstance("EC");  // Apple uses EC (Elliptic Curve) algorithmPrivateKey privateKey = keyFactory.generatePrivate(keySpec);JWTSigner signer = JWTSignerUtil.createSigner("ES256", privateKey);Map<String, Object> header = new HashMap<>();header.put("alg", "ES256"); // Algorithm used for signingheader.put("kid", weatherProperties.getKeyId());  // Key IDMap<String, Object> payload = new HashMap<>();payload.put("iss", weatherProperties.getTeamId());    // Team IDpayload.put("sub", weatherProperties.getBundleId());  // Issuer IDpayload.put("iat", now.getTime() / 1000); // Issued at time in secondspayload.put("exp", expiration.getTime() / 1000); // Expiration time in secondsString token = JWTUtil.createToken(header, payload, signer);// 提前60s过期RedisUtils.setCacheObject(cacheKey, token, Duration.ofSeconds(weatherProperties.getTokenTTL() - 60));return token;}return (String) cacheObject;}}

4.WeatherProperties

package com.engwe.app.trigger.controller.http.weather.properties;import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** @version 1.0* @description:* @date 2024/9/11 9:20*/
@Component
@ConfigurationProperties(prefix = "weather.auth.config")
@Getter
@Setter
public class WeatherProperties {private String key;private String keyId;private String teamId;private String bundleId;/*** 单位秒*/private Integer tokenTTL;private String languageCode;private String timezone;private String weatherEndpoint;private String availabilityEndpoint;}

5.nacos配置

拥有自己的苹果账号去苹果第三方的那个网址获取key这些东西

weather:auth:config:key: MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgHtq+c5C71dUVFQc5RWHzRRxjRqmnxpQ45pMakniZ2uugCgYIKoZIzj0DAQehRANCAAQkGzFLpI+WYcI13yunuPu1qQHBospCyVlxKaWKomCNli9ncA3HFr65qccqwk11AUPIetd5K0hh4MoNANRE94hHkeyId: JC75LXPCZ5teamId: M8PTMPM89HbundleId: com.engwe.apptokenTTL: 3600languageCode: entimezone: Asia/Shanghaidomain: https://weatherkit.apple.comweatherEndpoint: /api/v1/weatheravailabilityEndpoint: /api/v1/availability

6 IWeatherApi


import feign.Headers;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;import java.util.Map;/*** @author luxin@engwebikes.cn* @version 1.0* @description: TODO* @date 2024/9/12 15:26*/
@FeignClient(name = "weather-service", url = "${weather.auth.config.domain}")
public interface IWeatherApi {//    @GetMapping("/api/v1/weather/{lang}/{lat}/{lon}")@GetMapping("${weather.auth.config.weatherEndpoint}/{lang}/{lat}/{lon}")Map<String, Object> getWeatherByLocation(@RequestHeader("Authorization") String token, @PathVariable("lang") String lang, @PathVariable("lat") String lat, @PathVariable("lon") String lon, @RequestParam Map<String, String> params);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/148649.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

芯片仓管系统主要适用场景有哪些

随着科技产业的飞速发展&#xff0c;芯片作为电子设备的核心部件&#xff0c;其库存管理成为了企业运营中不可或缺的一环。芯片仓管系统&#xff0c;作为专为高效、精准管理芯片库存而设计的信息化工具&#xff0c;正逐步在多个关键领域展现出其不可或缺的价值。那么&#xff0…

APScheduler、Django、Python实现定时任务,以及任务操作

环境&#xff1a;Windows 11、python 3.12.3、Django 4.2.11、 APScheduler 3.10.4 背景&#xff1a;工作需要使用且用法较为复杂&#xff0c;各种功能基本都使用了 事件&#xff1a;20240920 说明&#xff1a;记录&#xff0c;方便后期自己查找 1、搭建基础环境 文件结构图…

免费分享一套SpringBoot+Vue火车票订票管理系统【论文+源码+SQL脚本】,帅呆了~~

大家好&#xff0c;我是java1234_小锋老师&#xff0c;看到一个不错的SpringBootVue火车票订票管理系统&#xff0c;分享下哈。 项目视频演示 【免费】SpringbootVue火车票订票管理系统 Java毕业设计_哔哩哔哩_bilibili 项目介绍 传统办法管理信息首先需要花费的时间比较多&…

基于Springboot个性化图书推荐系统JAVA|VUE|SSM计算机毕业设计源代码+数据库+LW文档+开题报告+答辩稿+部署教+代码讲解

源代码数据库LW文档&#xff08;1万字以上&#xff09;开题报告答辩稿 部署教程代码讲解代码时间修改教程 一、开发工具、运行环境、开发技术 开发工具 1、操作系统&#xff1a;Window操作系统 2、开发工具&#xff1a;IntelliJ IDEA或者Eclipse 3、数据库存储&#xff1a…

优思学院|TQM和ISO9001有什么关系?

TQM 和 ISO 9001有什么关系&#xff1f;我们把这个问题交给AI&#xff0c;它的回答是&#xff1a; 老实说&#xff0c;说它答错&#xff0c;又不算是错&#xff0c;但说它答对&#xff0c;也不算&#xff0c;总之就是一种模凌两可的感觉。 为什么会这样&#xff1f;因为管理中…

【gradio介绍】Python 可视化 web 神器---gradio介绍

Gradio是一个开源的Python库&#xff0c;专为帮助开发者快速搭建和分享机器学习模型、API或任意Python函数的用户界面&#xff08;UI&#xff09;而设计。它基于FastAPI和Svelte&#xff0c;是一个易于部署且功能强大的Web界面构建工具&#xff0c;特别适用于展示和测试机器学习…

在产品上扩大库存?教你一招!全开源!

几乎所有人都会遇到的头疼问题&#xff1a;内存不够&#xff0c;因为很多照片、音频、文档药存储。。。 我们都知道芯片的储存都是寸土寸金的&#xff0c;内部不够只能外扩&#xff01; 有没有简单一点的方法呢&#xff1f;实在不想编写各种驱动&#xff0c;替换Flash&#x…

探索未来:MultiOn,AI的下一个革命

文章目录 探索未来&#xff1a;MultiOn&#xff0c;AI的下一个革命背景&#xff1a;为什么选择MultiOn&#xff1f;MultiOn是什么&#xff1f;如何安装MultiOn&#xff1f;简单的库函数使用方法场景应用常见问题及解决方案总结 探索未来&#xff1a;MultiOn&#xff0c;AI的下一…

conda环境下module ‘numba.types‘ has no attribute ‘Macro‘问题解决

1 问题描述 conda环境下运行数据处理&#xff0c;报出如下错误&#xff1a; Traceback (most recent call last):File "train_preprocess.py", line 13, in <module>import audioFile "/opt/service/lipsync/audio.py", line 1, in <module>…

增强网络威胁防御能力的云安全新兴技术

一些行业专家强调了基于云的运营的独特网络安全需求&#xff0c;并指出保护敏感数据与传统的本地网络不同。尽管新兴技术并没有改变网络安全专业人员与犯罪分子之间持续的斗争&#xff0c;但它们提高了赌注&#xff0c;使斗争变得更加复杂。 如今&#xff0c;我们面对的是技术…

药用植物的空间多组学:从生物合成途径到工业应用-文献精读51

Spatial multi-omics in medicinal plants: from biosynthesis pathways to industrial applications 药用植物的空间多组学&#xff1a;从生物合成途径到工业应用 摘要 随着分子测序和成像技术的快速发展&#xff0c;药用植物的多组学研究进入了单细胞时代。我们讨论了空间多…

西安云仪:心无旁骛做实业 精益求精造仪表

仪器仪表&#xff0c;被誉为工业生产的“倍增器”&#xff0c;是国家测量精度和科技发展水平的重要体现。近年来&#xff0c;我国仪器仪表产业正在稳步增长。据统计&#xff0c;2023年实现营收10112亿元&#xff0c;正式进入万亿元时代&#xff0c;部分高端产品已经达到或接近国…

阿里云「通义灵码」迎来重磅升级,「AI 程序员」正式亮相!

最近两年&#xff0c;随着大语言模型和生成式 AI 技术的爆火&#xff0c;软件开发领域首当其冲成为了最热门的大模型应用场景之一&#xff0c;GitHub Copilot、通义灵码等 AI 辅助编程工具纷纷问世。这些工具通过自然语言处理和机器学习技术&#xff0c;能够理解开发者的意图&a…

USB转8路串口 USB转8路RS232 USB转8路TTL

一、功能描述 本模块采用CH348Q芯片作为主芯片&#xff0c;CH348 是一款高速 USB 总线的转接芯片&#xff0c;实现USB转八个异步串口UARTA/B/C/D/E/F/G/H 功能&#xff0c; 用于为计算机扩展异步串口&#xff0c;或者将普通的串口设备或者MCU直接升级到USB总线。外加4颗MAX323…

Leetcode 每日一题:Diameter of Binary Tree

写在前面&#xff1a; 最近被学校的 campus involvement 社团活动的招新宣传和选拔&#xff0c;以及找工作频繁的参加招聘会和网上申请忙的焦头烂额&#xff0c;马上又要到来的期中考试让我再次意识到了大学生活的险恶。虽然大家都说学生时代是最幸福的时代&#xff0c;但这个…

Vue3使用通信组件库mitt作为事件总线实现跨组件通信

mitt 介绍: Mitt 是一个在 Vue.js 应用程序中使用的小型事件总线库。该库允许组件进行通信&#xff0c;而不必过度依赖父级或子级组件之间的 props。 先看项目用例&#xff1a; 【 以下转载自&#xff1a;https://blog.csdn.net/yuanlong12178/article/details/139579299 】…

无人机飞手教员培训持证,必须会组装,模拟,维修才能带好学员

无人机飞手员的教培训不应仅仅局限于获取飞行执照或证书&#xff0c;而应是一个全面等多、方面的深入能力且&#xff0c;实践以确保导向能够的过程全面。、一个有效地合格的指导无人机学员飞。手教员不仅需要掌握扎实的飞行技能&#xff0c;还需要具备组装、模拟训练、维修。 组…

线性调频信号脉冲压缩并非是一个门信号

如果是频域是门信号&#xff0c;时域是sinc信号&#xff0c;时间越长震荡只会越小。图象是线性卷积做的&#xff0c;肯定没错。

SGLang——结构化语言模型程序的高效执行

前言 大型语言模型 (LLM) 越来越多地用于需要多次生成调用、高级提示技术、控制流和结构化输入/输出的复杂任务。然而&#xff0c;缺乏用于编程和执行这些应用程序的高效系统。新推出的系统 SGLang 旨在通过提供复杂语言模型程序的高效执行来解决这一问题。SGLang 包含前端语言…

828华为云征文|华为云Flexus X轻松实现Redis一主多从高效部署

目录 前言 一、华为云Flexus X加速Redis购买 1.1 Flexus X实例购买 1.2 Redis加速镜像选择 1.3 重置密码 1.4 登录Flexus X实例 1.5 Flexus X实例Redis验证 二、华为云Flexus X主节点Redis配置 2.1 重置密码 2.2 Redis外部访问配置 三、华为云Flexus X从节点Redis配置 3.1 从机…