From e8bd1357f3cf079bd198172ac9b0e5d5fcb18222 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 30 Apr 2016 00:00:59 +0200
Subject: [PATCH] More help to use REST API

---
 htdocs/api/admin/explorer.php | 20 +++++++++++++++++++-
 htdocs/api/index.php          |  2 ++
 htdocs/langs/en_US/admin.lang |  2 ++
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/htdocs/api/admin/explorer.php b/htdocs/api/admin/explorer.php
index 44a14f99675..66d06e91679 100644
--- a/htdocs/api/admin/explorer.php
+++ b/htdocs/api/admin/explorer.php
@@ -150,12 +150,30 @@ $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToM
 print load_fiche_titre($langs->trans("ApiSetup"),$linkback,'title_setup');
 
 
+// Define $urlwithroot
+$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
+$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT;		// This is to use external domain name found into config file
+//$urlwithroot=DOL_MAIN_URL_ROOT;					// This is to use same domain name than current
+
+// Show message
+print '<br>';
+$message='';
+$url='<a href="'.$urlwithroot.'/api/index.php/login?login='.urlencode($user->login).'&password=yourpassword" target="_blank">'.$urlwithroot.'/api/index.php/login?login='.urlencode($user->login).'&password=yourpassword</a>';
+$message.=$langs->trans("UrlToGetKeyToUseAPIs").':<br>';
+$message.=img_picto('','object_globe.png').' '.$url;
+print $message;
+print '<br>';
+print '<br>';
+
+print $langs->trans("ListOfAvailableAPIs").':<br>';
 foreach($listofapis['v1'] as $key => $val)
 {
+    if ($key == 'login') continue;
     if ($key)
     {
         //print $key.' - '.$val['classname'].' - '.$val['fullpath']." - ".DOL_MAIN_URL_ROOT.'/api/index.php/'.strtolower(preg_replace('/Api$/','',$val['classname']))."/xxx<br>\n";
-        $url=DOL_MAIN_URL_ROOT.'/api/index.php/'.$key;
+        $url=$urlwithroot.'/api/index.php/'.$key;
+        $url.='?api_key=token';
         print img_picto('','object_globe.png').' <a href="'.$url.'" target="_blank">'.$url."</a><br>\n";
         
     }
diff --git a/htdocs/api/index.php b/htdocs/api/index.php
index d7bcedb5fd7..55a45aea4b9 100644
--- a/htdocs/api/index.php
+++ b/htdocs/api/index.php
@@ -141,6 +141,8 @@ foreach ($modulesdir as $dir)
 
 // TODO If not found, redirect to explorer
 
+
+// Call API (we suppose we found it)
 $api->r->handle();
 
 
diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang
index b37083ca091..7b13a4ec5b5 100755
--- a/htdocs/langs/en_US/admin.lang
+++ b/htdocs/langs/en_US/admin.lang
@@ -1740,3 +1740,5 @@ AddOtherPagesOrServices=Add other pages or services
 AddModels=Add document or numbering templates
 AddSubstitutions=Add keys substitutions
 DetectionNotPossible=Detection not possible
+UrlToGetKeyToUseAPIs=Url to get token to use API (once token has been received it is saved on database user table and will be checked on each future access) 
+ListOfAvailableAPIs=List of available APIs
\ No newline at end of file
-- 
GitLab