微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用参数设计通用回调 url?

如何解决如何使用参数设计通用回调 url?

我有一项服务,它允许客户端创建回调 url,我的服务将使用该 url 调用 3rd 方网站。客户端提供的 url 模式会有所不同。 url 包含 student_id 和 age 参数。 假设客户端在我的服务中保存一个 url 为

https://www.client1.com/student/{student_id}:{age}

我的服务将调用 url

https://www.client1.com/student/12345:18

或者,如果提供的网址是

https://www.client2.com/update?student_id={student_id}&age={age}

我的服务会调用

https://www.client2.com/update?student_id=12345&age=18

我想支持多个 student_id 和 age,我应该如何使 url 更通用?例如,如何定义表示重复参数对的模式/模式?我认为一些需要支持的网址会像

https://www.client.com/update?00001:18,00002:15,00003:17 

https://www.client.com/update?students=00001,00002,00003&age=18,15,17

(其中 00001-00003 是学生,18、15、17 是年龄)

解决方法

这不是正则表达式问题,而是 API 架构问题。

要支持各种格式的重复模式,您可以定义 {repeat}...{/repeat} 架构或 {start_repeat}...{end_repeat} 架构。使用您的示例:

客户端 1 API 示例:

https://www.client1.com/update?students=00001,00002,00003&age=18,15,17

客户端 1 API 架构:

https://www.client1.com/update?students={repeat}{student_id}{/repeat}&age={repeat}{age}{/repeat}

客户端 2 API 示例:

https://www.client2.com/update?00001:18,00002:15,00003:17

客户端 2 API 架构:

https://www.client2.com/update?{repeat}{student_id}:{age}{/repeat}

然后,为了在您的末尾编写 URL,您全局提取 {repeat} ... {/repeat},并且对于每次重复,循环遍历您的学生,解析 {student_id} 和 {{1 }}。

此重复架构假定使用 {age} 分隔符。如果您的客户端有不同的分隔符,您可以使用 ,{comma_repeat}...{/comma_repeat} 增强架构。或者,保持重复模式不变,并在任何 {semicolon_repeat}...{/semicolon_repeat} 模式之前引入一个 {repeat_separator};{/repeat_separator}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。