ContentReceived patterns for dynamic endpoints

Hi,

I am working on an app that needs a URLConnection. When searching my ContentReceived event for a response, I need to check for dynamic Urls. For example i can make a call to /api/users and don’t have to worry about anything because it will always be the same. But what about /api/users/[user_id] where [user_id] changes. Example: /api/users/12345 or /api/users/09876.

Is there any pattern that the community has adopted? Below is an example.

Sub ContentReceived(URL As String, HTTPStatus As Integer, content As String)
     // Using logic to extract the endpoint from the URL only in the cases below

    Select Case URL
    Case "/users"
      OnUsersResponse(URL, HTTPStatus, content)
    Case "/users/12345" // <--- THIS WILL CHANGE DEPENDING ON THE USER
      OnUserResponse(URL, HTTPStatus, content)
    End Select
End Sub

TIA

[quote=“C P, post:1, topic:80277, username:C_P”]

Sub ContentReceived(URL As String, HTTPStatus As Integer, content As String)
     // Using logic to extract the endpoint from the URL only in the cases below

    If URL = "/users" then
      OnUsersResponse(URL, HTTPStatus, content)
    elseif URL.beginswith( "/users/") then // <--- THIS WILL CHANGE DEPENDING ON THE USER
      OnUserResponse(URL, HTTPStatus, content)
    End Select
End Sub
1 Like

Use multiple URLConnection objects so each response can have its own event/method. You’re essentially doing it already, just pushing into a Select statement first.

1 Like

Don’t forget that any method that returns the original string can be used in a select-case, so you could make a method that does regex matches and use that for special matches:

Function SelectRegex(aString as String, pattern as String) as String
    Var rx as new Regex
    Rx.searchpattern = pattern
    If rx.search(aString) <> nil then
        Return aString 
    End if
    Return aString + "_NOMATCH"
End Function

Then in your select case, you can do something like this:

Select Case URL
Case SelectRegex(URL, "^/users/[0-9]+/$")
    // user specific
End Select
2 Likes