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
RubberDuck
31.1k6 gold badges73 silver badges176 bronze badges
asked Apr 7, 2015 at 21:54
\$\endgroup\$

2 Answers 2

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
answered May 7, 2015 at 19:28
\$\endgroup\$
1
  • \$\begingroup\$ Very nice the_lotus, I really like that, much cleaner without having to repeat the tuples. \$\endgroup\$ Commented May 8, 2015 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.

answered Apr 7, 2015 at 22:07
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.