refactoring API URI building

 7th February 2024 at 5:45pm

Long day. A lot of commuting and I didn't have as much luck as yesterday, both with energy and bus spots. But it was a cheap trip, so there's that; and I had a second commute later in the day (in a train!) and in a couple of minutes I managed to make progress with creating the two API routes.

; API REQUESTS

; concatenates all fields into a string
; List[(key . value)] -> String
(define (create-field-string fields)
  (if (null? fields)
	""
	(let* ((key (caar fields))
		   (value (cadar fields))
		   (rest (create-field-string (cdr fields))))
	   (string-append "&" key "=" value rest))))

; manually creates URI string for an API request
; String List[(key . value)] -> String
(define (build-uri-with-fields base-uri fields)
  (string-append base-uri
				 (create-field-string fields)))

(test #t (string=? EXPECTED-SNATCHED-URI
				(build-uri-with-fields SNATCHED-URI
									   (list (list "id" (number->string USER-ID))
											 (list "type" "snatched")
											 (list "limit" (number->string NBR-OF-SNATCHES))))))
(test #t (string=? EXPECTED-GROUP-URI-WITH-ID-1234
				(build-uri-with-fields GROUP-URI
									   (list (list "id" (number->string 1234))))))

To work on this, I created another file, worked on it, and now I'm putting the contents back on the main file.

My main difficulty at this point is trying to organize the code in a sensible manner. I'm creating two different requests, which entails creating two different URI, etc. and the code duplicates fast; abstraction should, in fact, reduce the amount of code. It also doesn't help that I'm doing everything in the same file (testing included), and it gets cluttered easily. So tomorrow I'll clean up the code into different files.