3
\$\begingroup\$

I have a set of API endpoints where the URL is not standardized, so I could either get "api/auth" or "api/auth/" (may or may not end with "/"). I'm 'registering' the endpoint, plus the HTTP Method that comes in, so I can exclude certain endpoints from having to authenticate.

I've written this piece of code, which has a lot of repetition. Any thoughts?

Private _ExemptEndpoints As New List(Of Tuple(Of String, Method))
Public Sub RegisterAuthExemptEndpoint(endpoint As String, method As Method) Implements IRestAuthenticator.RegisterAuthExemptEndpoint

    Dim endpoints As New List(Of Tuple(Of String, Method))
    Dim allMethods As Boolean = False
    If method = Nothing Then allMethods = True

    Dim ep1
    Dim ep2

    If Not endpoint.EndsWith("/") Then
        ep1 = endpoint
        ep2 = endpoint + "/"
    Else
        ep1 = endpoint.Remove(endpoint.Length - 1, 1)
        ep2 = endpoint
    End If

    If allMethods Then
        _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep1, method.GET))
        _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep1, method.POST))
        _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep1, method.PUT))
        _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep1, method.DELETE))

        _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep2, method.GET))
        _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep2, method.POST))
        _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep2, method.PUT))
        _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep2, method.DELETE))
    Else
        _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep1, method))
        _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep2, method))
    End If

End Sub
\$\endgroup\$
3
\$\begingroup\$

The endpoints list doesn't seem to be used. Also, enable Option Explicit On.

You could have a list of method instead.

Dim methodToAdd As New List(Of Method)

If method = Nothing Then
    ' This list could be stored
    methodToAdd.Add(Method.GET)
    methodToAdd.Add(Method.POST)
    methodToAdd.Add(Method.PUT)
    methodToAdd.Add(Method.DELETE)
Else
    methodToAdd.Add(method)
End If

Dim ep1, ep2 As String

If Not endpoint.EndsWith("/") Then
    ep1 = endpoint
    ep2 = endpoint + "/"
Else
    ep1 = endpoint.Remove(endpoint.Length - 1, 1)
    ep2 = endpoint
End If

For Each m As Method In methodToAdd
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep1, m))
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep2, m))
Next
\$\endgroup\$
  • \$\begingroup\$ Very nice the_lotus, I really like that, much cleaner without having to repeat the tuples. \$\endgroup\$ – Greg May 8 '15 at 22:47
3
\$\begingroup\$

Basically what you are asking is what can we do about this

    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep1, method.GET))
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep1, method.POST))
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep1, method.PUT))
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep1, method.DELETE))

    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep2, method.GET))
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep2, method.POST))
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep2, method.PUT))
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep2, method.DELETE))

and the contents of the else block as well.

I would create a method that takes the Endpoints ep1 and ep2 as parameters

something like

Public Sub MethodAddingEndpointsToExemptedList(endpoint As String)
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(endpoint, method.GET))
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(endpoint, method.POST))
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(endpoint, method.PUT))
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(endpoint, method.DELETE))
End Sub

and then you can just call this method inside the if block like this

If allMethods Then
    MethodAddingEndpointsToExemptedList(ep1)
    MethodAddingEndpointsToExemptedList(ep2)
Else
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep1, method))
    _ExemptEndpoints.Add(Tuple.[New](Of String, Method)(ep2, method))
End If

You could go a step further and create a list of the endpoints and then use a foreach loop to add them to the list of Exempted endpoints.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.