Module Web Services API REST (developer): Difference between revisions

Jump to navigation Jump to search
m
(→‎Use: sqlfilters example)
Tag: 2017 source edit
Line 21: Line 21:




= Function =
=Function=
When enabling this module, you enable usage of web services provided by Dolibarr server. You can then make REST call of different web services provided by Dolibarr.
When enabling this module, you enable usage of web services provided by Dolibarr server. You can then make REST call of different web services provided by Dolibarr.


= Dolibarr REST web services server =
=Dolibarr REST web services server=
Once module webservices REST is activated, Dolibarr become also a server of REST web services. So you can send your own REST request to relative URL
Once module webservices REST is activated, Dolibarr become also a server of REST web services. So you can send your own REST request to relative URL
'''/api/index.php/xxx''' where xxx is name of the API to call.
'''/api/index.php/xxx''' where xxx is name of the API to call.
Line 38: Line 38:
However, NGinx default setup on a lot of distribution is often a gaz engine and you may experience troubles. If you experience suche troubles, you can try to edit your NGinx config file to match the following example. This is a simple localhost setup with a working REST API handling (tested with 9.0.1 on parabola gnu/linux).
However, NGinx default setup on a lot of distribution is often a gaz engine and you may experience troubles. If you experience suche troubles, you can try to edit your NGinx config file to match the following example. This is a simple localhost setup with a working REST API handling (tested with 9.0.1 on parabola gnu/linux).


<syntaxHighlight lang="nginx">
<syntaxhighlight lang="nginx">
worker_processes  1;
worker_processes  1;
error_log  /var/log/nginx/error.log;
error_log  /var/log/nginx/error.log;
Line 93: Line 93:
     }
     }
}
}
</syntaxHighlight>
</syntaxhighlight>


= List of provided services =
=List of provided services=
Only few services are available. Starting to Dolibarr 5.0 version, you can see full list of Dolibarr Web services provided, by calling the explorer here at address:
Only few services are available. Starting to Dolibarr 5.0 version, you can see full list of Dolibarr Web services provided, by calling the explorer here at address:


Line 112: Line 112:
You can then test directly from this explorer any API. This is the recommended solution to test any Dolibarr API since any API and parameters is documented here. As a result of any test, you will get the answer but also example on how to call the API from command line using '''curl'''.
You can then test directly from this explorer any API. This is the recommended solution to test any Dolibarr API since any API and parameters is documented here. As a result of any test, you will get the answer but also example on how to call the API from command line using '''curl'''.


= Installation =
=Installation=


To install, open the modules page and activate de API REST module. On the configuration page of the module, there's a link to an explorer. Click on it to open the API explorer.
To install, open the modules page and activate de API REST module. On the configuration page of the module, there's a link to an explorer. Click on it to open the API explorer.
Line 122: Line 122:
On this exploration page of the API, you can do quite a lot of tests. Reading datas from Dolibarr and writing, modifying and deleting as well. Warning: Data are really modified in your database.
On this exploration page of the API, you can do quite a lot of tests. Reading datas from Dolibarr and writing, modifying and deleting as well. Warning: Data are really modified in your database.


= Use =
=Use=


To use the REST API,  you have to call an url such as this one : <nowiki>http://<my_server>/api/index.php/<action></nowiki><br/>
To use the REST API,  you have to call an url such as this one : <nowiki>http://<my_server>/api/index.php/<action></nowiki><br />
with one of the 4 following methods : GET, POST, PUT, DELETE, replacing <action> by the action you want to use. Ex : <nowiki>http://<my_server>/api/index.php/invoices</nowiki>
with one of the 4 following methods : GET, POST, PUT, DELETE, replacing <action> by the action you want to use. Ex : <nowiki>http://<my_server>/api/index.php/invoices</nowiki>


Line 130: Line 130:




<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
// Example of function to call a REST API
// Example of function to call a REST API
function callAPI($method, $apikey, $url, $data = false)
function callAPI($method, $apikey, $url, $data = false)
Line 174: Line 174:


     return $result;
     return $result;
}</syntaxHighlight>
}</syntaxhighlight>




This is just a working example. There's no error control and security was not in mind but you can use this code, modify it to suit your needs<br/>
This is just a working example. There's no error control and security was not in mind but you can use this code, modify it to suit your needs<br />
The fonction has 4 parameters :
The fonction has 4 parameters :
* $method : string, "GET", "POST", "PUT", "DELETE"
 
* $apikey : string, "your <token> generated earlier"
*$method : string, "GET", "POST", "PUT", "DELETE"
* $url : string, url to call. Ex : "<nowiki>http://<my_server>/api/index.php/invoices</nowiki>"
*$apikey : string, "your <token> generated earlier"
* $data : string, datas in json format. This parameter is not mandatory.
*$url : string, url to call. Ex : "<nowiki>http://<my_server>/api/index.php/invoices</nowiki>"
*$data : string, datas in json format. This parameter is not mandatory.


==Accepted parameters==
==Accepted parameters==
This is documented somewhat in the REST Explorer. Check that first. Second try and see what response you get. Third look at the source code for the given API.
This is documented somewhat in the REST Explorer. Check that first. Second try and see what response you get. Third look at the source code for the given API.


=== sqlfilters ===
===sqlfilters===
They are passed on to the database after a check.
They are passed on to the database after a check.
These work on the product endpoint:
These work on the product endpoint:
* (t.fk_product_type: = :'0') and (t.tosell: = :'1') and (t.label: ilike :'%string')
 
*(t.fk_product_type: = :'0') and (t.tosell: = :'1') and (t.label: ilike :'%string')


==Date==
==Date==
The ISO date format is accepted: %Y-%m-%d e.g. 2020-07-14. yymmdd is not accepted.
The ISO date format is accepted: %Y-%m-%d e.g. 2020-07-14. yymmdd is not accepted.


= Examples with PHP =
=Examples with PHP=


This is more examples, for different uses cases:
This is more examples, for different uses cases:


In every examples, we have:
In every examples, we have:
* $apiKey = "<my token>";
* $apiUrl = "<nowiki>http://<my_server>/api/index.php/</nowiki>";


<syntaxHighlight lang="php">
*$apiKey = "<my token>";
*$apiUrl = "<nowiki>http://<my_server>/api/index.php/</nowiki>";
 
<syntaxhighlight lang="PHP">
// Retrieve products list
// Retrieve products list
$listProduits = [];
$listProduits = [];
Line 216: Line 219:
}
}
}
}
</syntaxHighlight>
</syntaxhighlight>
Comments :
Comments :
* I retrieve the 10'000 first products sorted on their ID in the base
 
* html_entity_decode is necessary as single quotes are encoded
*I retrieve the 10'000 first products sorted on their ID in the base
* It's easy to use the same method (replacing products with dictionnarycountries) to retrieve the list of countries and their ID
*html_entity_decode is necessary as single quotes are encoded
*It's easy to use the same method (replacing products with dictionnarycountries) to retrieve the list of countries and their ID




<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
// Create a product
// Create a product
$ref = "my_product_ref_X203ZZ";
$ref = "my_product_ref_X203ZZ";
Line 232: Line 236:
$newProductResult = CallAPI("POST", $apiKey, $apiUrl."products", json_encode($newProduct));
$newProductResult = CallAPI("POST", $apiKey, $apiUrl."products", json_encode($newProduct));
$newProductResult = json_decode($newProductResult, true);
$newProductResult = json_decode($newProductResult, true);
</syntaxHighlight>
</syntaxhighlight>
Comments :
Comments :
* before creating a product, it could be wise to check if it exists. Using my first example, you'll have  :


*before creating a product, it could be wise to check if it exists. Using my first example, you'll have  :


<syntaxHighlight lang="php">
 
<syntaxhighlight lang="PHP">
// my ref
// my ref
$ref = "my_product_ref_X203ZZ";
$ref = "my_product_ref_X203ZZ";
Line 264: Line 269:
}
}
}
}
</syntaxHighlight>
</syntaxhighlight>
Comments :
Comments :
* I check if the ref of my product exist in the array created in the first example.
 
* If it exists, I use it's key in the array as ID
*I check if the ref of my product exist in the array created in the first example.
* If it doesn't exist, I create the product, then I add it to my array for next time then I use the ID created
*If it exists, I use it's key in the array as ID
* I choose this method to do less API calls when I have to import 500 orders. I just need to retrieve the products list once at the beginning instead of searching Dolibarr each time.
*If it doesn't exist, I create the product, then I add it to my array for next time then I use the ID created
*I choose this method to do less API calls when I have to import 500 orders. I just need to retrieve the products list once at the beginning instead of searching Dolibarr each time.


Warning: The example below is not working properly. Creating an order and lines in one go is not supported. Instead you have to create an order first and with the returned order id add the lines using the /orders/{id}/lines endpoint.  
Warning: The example below is not working properly. Creating an order and lines in one go is not supported. Instead you have to create an order first and with the returned order id add the lines using the /orders/{id}/lines endpoint.  


<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
// create an order with 2 products
// create an order with 2 products


Line 317: Line 323:
$newCommandeResult = json_decode($newCommandeResult, true);
$newCommandeResult = json_decode($newCommandeResult, true);
}
}
</syntaxHighlight>
</syntaxhighlight>
Comments :
Comments :
* $clientDoliId is the ID of the customer in the Dolibarr database. Either you know it or you can search for it before
* type => 0, This is a customer order (while 1 = supplier order)


*$clientDoliId is the ID of the customer in the Dolibarr database. Either you know it or you can search for it before
*type => 0, This is a customer order (while 1 = supplier order)


<syntaxHighlight lang="php">
 
<syntaxhighlight lang="PHP">
// Validate an order  
// Validate an order  
$newCommandeValider = [
$newCommandeValider = [
Line 332: Line 339:
$newCommandeValiderResult = json_decode($newCommandeValiderResult, true);
$newCommandeValiderResult = json_decode($newCommandeValiderResult, true);


</syntaxHighlight>
</syntaxhighlight>
Comments :
Comments :
* in this example, on the penultimate line, we can see : $apiUrl."orders/".$newCommandeResult."/validate".<br/>
 
*in this example, on the penultimate line, we can see : $apiUrl."orders/".$newCommandeResult."/validate".<br />
 
$newCommandeResult is the ID of the order created in the previous example
$newCommandeResult is the ID of the order created in the previous example




<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
// search in the database if a customer exist
// search in the database if a customer exist
$customer_name = "Acme Inc";
$customer_name = "Acme Inc";
Line 350: Line 359:
)
)
), true);
), true);
</syntaxHighlight>
</syntaxhighlight>
Comments :
Comments :
* limit => 1 only 1 customer
* mode => 1 we are looking for a customer, not a supplier (they are thirdparties too but with a different status)
* sqlfilters a bit special but there are more examples on the explorer page


*limit => 1 only 1 customer
*mode => 1 we are looking for a customer, not a supplier (they are thirdparties too but with a different status)
*sqlfilters a bit special but there are more examples on the explorer page


<syntaxHighlight lang="php">
 
<syntaxhighlight lang="PHP">
// customer doesn't exist. Let's create it and get it's ID
// customer doesn't exist. Let's create it and get it's ID
$newClient = [
$newClient = [
Line 368: Line 378:
$newClientResult = json_decode($newClientResult, true);
$newClientResult = json_decode($newClientResult, true);
$clientDoliId = $newClientResult;
$clientDoliId = $newClientResult;
</syntaxHighlight>
</syntaxhighlight>
Comments :
Comments :
* client => 1 He is a customer (not a supplier)
* code_client => -1 so the customer code will be generated automatically.
* we get the customer ID in $clientDoliId


= Examples with Python =
*client => 1 He is a customer (not a supplier)
*code_client => -1 so the customer code will be generated automatically.
*we get the customer ID in $clientDoliId
 
=Examples with Python=
See https://pypi.org/project/dolipy/ and https://pypi.org/project/dolibarr/
See https://pypi.org/project/dolipy/ and https://pypi.org/project/dolibarr/


= Develop your own service / API =
=Develop your own service / API=
Adding a new REST service is as easy than adding a file called '''api_mymoduleobject.class.php''' into the directory '''htdocs/mymodule/class'''.
Adding a new REST service is as easy than adding a file called '''api_mymoduleobject.class.php''' into the directory '''htdocs/mymodule/class'''.
You take the example into '''htdocs/commande/class/api_orders.class.php'''
You take the example into '''htdocs/commande/class/api_orders.class.php'''
Line 390: Line 401:
These are just the basics.
These are just the basics.


Some more informations in the Dolibarr code. You can look here : htdocs/<directory>/class/api_xxx_class.php<br/>
Some more informations in the Dolibarr code. You can look here : htdocs/<directory>/class/api_xxx_class.php<br />
Ex : htdocs/societe/class/api_thirdparties.class.php for thirdparties.<br/>
Ex : htdocs/societe/class/api_thirdparties.class.php for thirdparties.<br />
Ex : Invoices are in htdocs/compta/facture/class<br/>
Ex : htdocs/societe/class/api_contacts.class.php for contacts.<br />
Ex : htdocs/compta/facture/class/api_invoice_class.php for invoices.<br />
Ex : htdocs/mrp/class/api_mos_class.php for manufacturing orders.<br />
Ex : htdocs/product/class/api_products.class.php for contacts.<br />

Navigation menu