URLConnection Implementation Best Practice

Hi, I am trying to create a class to connect to a rest service. I basically need to connect to a rest api for a db. Since the service i am using does not have a SDK for Xojo, i am trying to create one for me for Mac and Window and maybe to share it with others.

As i am creating this class, i have some questions on how to implement it since i will need a network connection to the service.

My first idea was to make a class for my db and then instantiate a URLConnection (sync) anytime i had to connect to the db service. This i believe it is not the best practice since i read the xojo docs and they mentioned that using sync can “Freeze” my app. Also this does not work well for responses since i can’t get status,error or things like that. At least what i have tried.

The second way of doing this was to drag a URLConnection class and modify it to my needs but then I will need to write an if statement (possibly long) for each of the responses that i get back in the ContentReceived event. I noticed that by doing this, i run into some issues with sticky headers and content. Also i need to drag this object to my window to use it. Not a bad idea but i want to have a better design for implementation.

The third way of doing this is to create a class for my db service. Then in any method of my db class where i need a network connection to the service, i can create an instance of the URLConnection. At this point this implementation is not bad, the only problem is that I now have to use the addHandler and the addressOf to pass a custom method that i have created in the window where i am using the db class instance so i can get the response or the error. This is not a good design because i feel this is very coupled.

The last version is to create the db class, use a URLConnection instance and then pass somehow (not sure how to do this) a function (for ContentReceived) to any method where i need to use the URLConnection. This way the function can be pass to the addHandler and addressOf and at least is a little decoupled.

Sorry for the question but i see many tutorials teaching you how to use the URLConnection and most of them just drag the object to the window where they need it. The problem is that not all use cases are that simple. If you have any examples (real life) in github i can see that will be awesome. Thanks for all your time and support.

Let me know what you think. I am eager to learn what is the best practice to do this. By the way, i could be describing something wrong in the above comment but i am writing this with my limited understanding of OOP (hence i am tyring to get better with OOP).

@C_P first thing is that this subject should be in the general forum not here.

Unfortunately, my clients don’t allow sharing of code so I can’t send you to a Git repository.

However, you should look at using interfaces and I would recommend looking at the Observer Design Pattern for a good example.

Essentially you create a class for each table in your database (these are generally called ORM classes or Object Relational Matching) that deal with a row in your table - each class has a property that matches a column in your table and each class object matches a row in your table.

By implementing an interface, you essentially set the Super of your object to a second (or more) object type. So, all classes that implement the interface can also be referred to by that interface name.

So, all ORM classes that implement the “DataRecieved” interface can also be treated as DataRecieved Objects regardless of their names.

Sorry this is complex, but you asked :slight_smile:

2 Likes

A fourth way is to subclass a URLConnection for each of the REST calls you need to make. You handle the raw content in ContentReceived and raise an abstracted event higher up in the application

You can download an example here
https://www.emscom.net/download/xojo/whatsmyip.xojo_binary_project

The example uses a minimal amount of code to demonstrate some useful OOP techniques that don’t get a lot of coverage in the tutorials.

(Embedding hostnames in an application is not good practice.)

1 Like