this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: extract parseClientFilter() helper

DRY up identical client filter parsing block that was
duplicated across links, quotes, and search handlers.

+26 -35
+20
internal/handler/api_v1_helpers.go
··· 7 7 "net/http" 8 8 "strconv" 9 9 "strings" 10 + 11 + "tumble/internal/data" 10 12 ) 11 13 12 14 // writeJSON writes a JSON response with the given status code and data. ··· 125 127 return strings.TrimSuffix(path, ".txt") 126 128 } 127 129 return path 130 + } 131 + 132 + // parseClientFilter extracts client filter parameters from the request query string. 133 + func parseClientFilter(r *http.Request) (data.ClientFilter, error) { 134 + var f data.ClientFilter 135 + if st := r.URL.Query().Get("client_type"); st != "" { 136 + f.ClientType = &st 137 + } 138 + if sn := r.URL.Query().Get("client_network"); sn != "" { 139 + f.ClientNetwork = &sn 140 + } 141 + if sc := r.URL.Query().Get("client_channel"); sc != "" { 142 + f.ClientChannel = &sc 143 + } 144 + if err := f.Validate(); err != nil { 145 + return f, err 146 + } 147 + return f, nil 128 148 } 129 149 130 150 // isAuthorizedAPIKey checks if the request has a valid API key.
+2 -11
internal/handler/api_v1_links.go
··· 71 71 offset := parseIntParam(r, "offset", 0, 1000000) 72 72 73 73 // Parse client filter query params 74 - var clientFilter data.ClientFilter 75 - if st := r.URL.Query().Get("client_type"); st != "" { 76 - clientFilter.ClientType = &st 77 - } 78 - if sn := r.URL.Query().Get("client_network"); sn != "" { 79 - clientFilter.ClientNetwork = &sn 80 - } 81 - if sc := r.URL.Query().Get("client_channel"); sc != "" { 82 - clientFilter.ClientChannel = &sc 83 - } 84 - if err := clientFilter.Validate(); err != nil { 74 + clientFilter, err := parseClientFilter(r) 75 + if err != nil { 85 76 writeAPIError(w, http.StatusBadRequest, "invalid_params", err.Error()) 86 77 return 87 78 }
+2 -11
internal/handler/api_v1_quotes.go
··· 71 71 offset := parseIntParam(r, "offset", 0, 1000000) 72 72 73 73 // Parse client filter query params 74 - var clientFilter data.ClientFilter 75 - if st := r.URL.Query().Get("client_type"); st != "" { 76 - clientFilter.ClientType = &st 77 - } 78 - if sn := r.URL.Query().Get("client_network"); sn != "" { 79 - clientFilter.ClientNetwork = &sn 80 - } 81 - if sc := r.URL.Query().Get("client_channel"); sc != "" { 82 - clientFilter.ClientChannel = &sc 83 - } 84 - if err := clientFilter.Validate(); err != nil { 74 + clientFilter, err := parseClientFilter(r) 75 + if err != nil { 85 76 writeAPIError(w, http.StatusBadRequest, "invalid_params", err.Error()) 86 77 return 87 78 }
+2 -13
internal/handler/api_v1_search.go
··· 3 3 import ( 4 4 "net/http" 5 5 "strings" 6 - 7 - "tumble/internal/data" 8 6 ) 9 7 10 8 // APIv1SearchHandler handles GET /api/v1/search ··· 64 62 offset := parseIntParam(r, "offset", 0, 1000000) 65 63 66 64 // Parse client filter query params 67 - var clientFilter data.ClientFilter 68 - if st := r.URL.Query().Get("client_type"); st != "" { 69 - clientFilter.ClientType = &st 70 - } 71 - if sn := r.URL.Query().Get("client_network"); sn != "" { 72 - clientFilter.ClientNetwork = &sn 73 - } 74 - if sc := r.URL.Query().Get("client_channel"); sc != "" { 75 - clientFilter.ClientChannel = &sc 76 - } 77 - if err := clientFilter.Validate(); err != nil { 65 + clientFilter, err := parseClientFilter(r) 66 + if err != nil { 78 67 writeAPIError(w, http.StatusBadRequest, "invalid_params", err.Error()) 79 68 return 80 69 }