assembling the record info into a struct

 7th February 2024 at 6:43pm

After the refactor, I can now focus on going a little further with the code. I can already get the list of latest downloaded records, which provides an internal groupId, which I later use on another request to get information on each specific record.

The API request to the groupId endpoint can be parsed with something like

; retrieves a list of information in the record from the intarweb#response
; groupid-route-response -> record-info
(define (get-record-info-from-groupid-response json-response)
	(make-record-info 
		title: (cdr (assoc 'name (cdr (cadadr json-response))))
		year: (cdr (assoc 'year (cdr (cadadr json-response))))
    [...]

but the artists come in a different structure: a vector. In this particular example, #(((id . 81) (name . "Kraftwerk"))); but in the case of very memorable records that are collaborations — SCARING THE HOES, Danny Brown & JPEGMAFIA, for example 🤭 —, this must be flattened into a single string.

; retrieves the artists from a groupid-route-response
; groupid-route-response -> ArtistsVector
(define (retrieve-artists-from-group-response response)
	(cdr (assoc 'artists (cdr (assoc 'musicInfo (cdr (cadadr response)))))))

; creates a string from the artist vector
; ArtistsVector -> String
(define (assemble-artist-string vec)
  (reduce (lambda (a b) (string-append a ", " b))
		  ""
		  (map (lambda (pair) (cdr (assoc 'name pair)))
			   (vector->list vec))))
(test "can retrieve artist from solo album"
	  #t
	  (string=? (assemble-artist-string (retrieve-artists-from-group-response grouprecord-1234-response))
				"Kraftwerk"))
(test "can retrieve artists from group album"
	  #t
	  (string=? (assemble-artist-string (retrieve-artists-from-group-response grouprecord-1926431-response))
				"JPEGMAFIA, Danny Brown"))

and, finally,

;retrieves a list of information in the record from the intarweb#response
;groupid-route-response -> record-info
(define (get-record-info-from-groupid-response json-response)
	(make-record-info 
		title: (cdr (assoc 'name (cdr (cadadr json-response))))
		year: (cdr (assoc 'year (cdr (cadadr json-response))))
		artist: (assemble-artist-string (retrieve-artists-from-group-response json-response))))
(test "creating record-info struct from groupid-route-response"
	  #t
	  (let ((test-record-info (get-record-info-from-groupid-response grouprecord-1234-response)))
		(and (record-info? test-record-info)
			 (string=? (record-info-artist test-record-info) "Kraftwerk")
			 (string=? (record-info-title test-record-info) "Trans-Europe Express (Trans Europa Express)")
			 (string=? (record-info-year test-record-info) "1977"))))

From here onward, it's just a matter of connecting all the pieces together: for each recently downloaded record (obtained from the snatched-response), create a record-info struct.