Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

regex - How to design a generic callback url with params?

I have a service which allows client to create a callback url and my service will use that url to call a 3rd party website. The url pattern that the clients provide would be different. The url contains params of student_id and age. Assume that the client save a url in my service as

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

My service will call the url with

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

or, if the url provided is

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

My service will call

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

I want to support multiple student_id and age, how should I make the url more generic? Such as, how to define a pattern/schema that represent repeating pair of params? I think some urls that need to be supported would be like

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

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

(where 00001-00003 are studentids, 18,15,17 are ages)

question from:https://stackoverflow.com/questions/65887057/how-to-design-a-generic-callback-url-with-params

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is not a regex question, rather an API schema question.

To support repeating patterns of various formats you can define a {repeat}...{/repeat} schema, or a {start_repeat}...{end_repeat} schema. Using your examples:

Client 1 API example:

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

Client 1 API schema:

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

Client 2 API example:

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

Client 2 API schema:

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

Then, to compose the URL on your end, you globally extract {repeat} ... {/repeat}, and for each repeat, loop through your students, resolving {student_id} and {age}.

This repeat schema assumes a , separator. If your clients have different separators, you could enhance the schema with {comma_repeat}...{/comma_repeat}, {semicolon_repeat}...{/semicolon_repeat}. Alternatively, keep the repeat schema as is, and introduce a {repeat_separator};{/repeat_separator} that should preceded any {repeat}...{/repeat} patterns.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...