Discogs API call with Xojo

I wrote some software that fetches data from the Discogs music website through the API they provide for that. I have it working currently, but I have a feeling my code is very crude. Whenever I try code the way Xojo recommends in their docs, it stops working, so for this time I keep using my code:

searchstring = "catno=" + catno + "&"
if len(Title_Aside) > 1 then
  searchstring = searchstring + "title=" + trim(Title_Aside)
end if
if len(Artist_Aside) > 1 then
  searchstring = searchstring + "artist=" + trim(Artist_Aside)
end if
temp = "https://api.discogs.com/database/search?"+searchstring+"key=111&secret=222&per_page=100"

//Discogs zoeken op catno en dit in string zetten
try
  JSONtext = WebConnection.SendSync("GET", temp, 30)
  // Catch Err as NetworkException
Catch Err as RuntimeException
  MessageBox "There was an error"
end try

Is there a way to do this more cleanly, like with an array (the way Xojo advises) or is this method quite normal procedure?

You are missing “&” at the end of your title and artist parameters.

I would write it like this:

Var search() as string 

Search.add "catno=" + catno

if len(Title_Aside) > 1 then
  search.add "title=" + trim(Title_Aside)
end if
if len(Artist_Aside) > 1 then
  search.add "artist=" + trim(Artist_Aside)
end if

temp = "https://api.discogs.com/database/search?"+_
string.fromArray(search,"&") +_
"&key=111&secret=222&per_page=100"

(written on my phone so there might be mistakes in the code)

2 Likes

Ah, you’re right, my mistake while typing over my code (it was there in the original code).

I also suggest using EncodeURLComponent on the values to the right of the equals signs.

1 Like

:rofl: OMG, there’s a Xojo command for that!? Using Xojo/RealBasic since 2004 and never knew that (or forgot about it).

In another part of this project I’m using the Folderitem.PathtypeURL to open music files and I’m using a bunch of replaceall commands to replace the unsafe characters in the full path. Never too old to learn, I guess. Thanks!

I also added content-type and user-agent headers since Discogs requests that. The code turned out like this:

Var socket As New URLConnection
socket.RequestHeader("Content-Type") = "application/json"
socket.RequestHeader("user-agent") = "Jukehead/1.1"

zoekstring = "catno="+catno+"&"
if len(TFTitel.Text) > 1 then
  zoekstring = zoekstring+"title="+trim(TFTitel.text).ReplaceAll(" ","+")+"&"
end if
if len(TFArtiest.text) > 1 then
  zoekstring=zoekstring+"artist="+trim(TFArtiest.text).ReplaceAll(" ","+")+"&"
end if
zoekURL = "https://api.discogs.com/database/search?"_
+zoekstring+"key=111&secret=222&per_page=100"

try
  JSONtext = socket.SendSync("GET", zoekURL, 30)
Catch Err as RuntimeException
  DiscogsConnected = False
end try