1

Okay so I'm using this API https://github.com/donbalon4/API_Alfresco to upload files to Alfresco from some other PHP site of another service. I don't have any code issue, it's just that as I was using it, some doubts on how to correctly use an API came up to my mind.

I'm basically using it to create some directories into my Alfresco Share. As I said, it's working perfectly well, so the issue is not about my code but on how to use the API (or any API in general) correctly.

First off, if you take a look to that API you'll see how every method is handling exceptions, i.e. if you try to create a Folder that already exists, then an Exception will be thrown with proper information.

So far I'm just using the API to create multiple directories into some parent root, and some of those directories are likely to have the same name. So when they do, I simply don't have to create them (since directory already exists) and don't need any exception to be thrown or be shown to the end user.

What's the best approach to this goal? I don't feel like modifying the API since it would be contradicting its real meaning. If everyone modifies an existent API then it's not an API anymore. I could simply remove exception handling of methods I'm using, but I don't think it would be a great practice.

Also I realized there's a private method called existsFolder() for internal use. I could maybe change it into public so I can use it on my actual PHP code to check myself if a given folder already exists, but I don't really know if it would be a great approach either.

What would be the best way to manage directories that already exist? Replace exception handling with a text message to be shown to the end user? Or making the existsFolder() method public? Any other idea I could be missing?

Miquel Perez
  • 445
  • 1
  • 6
  • 17
  • Why not use CMIS to talk to Alfresco? Apache Chemistry provides a good client library you can use from PHP - http://chemistry.apache.org/php/phpclient.html – Gagravarr Aug 08 '17 at 14:28
  • That's exactly what I'm using! API_Alfresco (link I provided in the post) actually uses Apache Chemistry. It just makes it a bit easier to perform basic file/folder operations to Alfresco through CMIS (and Apache Chemistry). – Miquel Perez Aug 08 '17 at 18:21

1 Answers1

0

If the API throws an exception your code should catch it and then act accordingly. Your code needs to surround any of the API calls that might throw an exception with a try-catch clause. For example, if your code sees that an exception was thrown because a folder already exists and you don't care, you can just swallow the exception and move on.

Jeff Potts
  • 10,468
  • 17
  • 40
  • I agree but in that case the API was throwing an exception when making an HTTP request with an URL containing special characters. I think that rather than throwing an exception it'd be better if the API handled special chars and made proper string treatment to ensure HTTP requests wont fail. Indeed I fixed that in the API and started a pull request that is now merged with the master branch so it is now fixed. Simply throwing an exception can be a pretty good approach to an API but I think it is always better to give the programmer the best experience and have him do less work as possible. – Miquel Perez Aug 19 '17 at 10:47