Skip to content

Commit ba5999f

Browse files
authored
Merge pull request #2113 from jolelievre/admin-api-setup-dev-environement
Add page that guides the dev to setup deve environment for Admin API
2 parents ceb3700 + 2372996 commit ba5999f

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

admin-api/contribute-to-core-api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ If any issues are detected, the CI will provide helpful comments to guide you on
4242
- Understanding of [API Resources and our customer operations]({{< relref "/9/admin-api/resource_server/api-resources">}})
4343
- Understanding the conventions defined in our [CQRS API guidelines ADR](https://github.com/PrestaShop/ADR/blob/master/0023-cqrs-api-guidelines.md) (Architecture Decision Record)
4444

45+
{{% notice note %}}
46+
**Setup development environment**
47+
48+
To setup an environment where you can easily add new endpoints you can [follow this guide]({{< relref "/9/admin-api/setup-development-environment">}}).
49+
{{% /notice %}}
50+
4551
## 🎯 Objective
4652

4753
Create REST API endpoints to manage attribute groups (AttributeGroup) with complete CRUD operations and comprehensive PHPUnit integration test coverage.

admin-api/img/api-client-list.png

54.1 KB
Loading
34.8 KB
Loading

admin-api/img/swagger-link.png

78.8 KB
Loading

admin-api/img/swagger-login.png

159 KB
Loading
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
title: Setup development environment
3+
showOnHomepage: false
4+
weight: 250
5+
---
6+
7+
# Setup development environment
8+
9+
This guide will show you how to setup a PrestaShop instance with the `ps_apiresources` module replaced with a Git folder and linked to your fork so you can add new endpoints.
10+
11+
It is based on docker so a maximum of people can follow it regardless of their environment, but you can setup your environment differently and locally if you prefer.
12+
13+
## 📋 Prerequisites
14+
15+
- Docker
16+
- Git
17+
18+
## 🎯 Objective
19+
20+
- Install a full shop locally so you can request the Admin API
21+
- Replace the `ps_apiresources` module folder (installed by composer) with a Git folder, so you can create pull requests.
22+
- Setup an API client with basic authorization so you can use it to test the Admin API
23+
24+
## 🏗 Setup docker environment
25+
26+
### Setup the shop
27+
28+
We base this guide on the `9.0.x` branch but you can use a more recent branch if you want.
29+
30+
```bash
31+
git clone -b 9.0.x [email protected]:PrestaShop/PrestaShop.git prestashop-90x
32+
cd prestashop-90x
33+
make docker-start
34+
# The installation runs in background and takes a few minutes, you can run this command to see its progress
35+
make docker-logs
36+
# Once you see the message "Starting web server now" you're good to go
37+
```
38+
39+
You should now be able to access your PrestaShop installation
40+
- Frontend: http://localhost:8001
41+
42+
- Password: 123456789
43+
- Backend: http://localhost:8001/admin-dev
44+
45+
- Password: Correct Horse Battery Staple
46+
- Email testing: http://localhost:1080
47+
48+
### Setup a dev environment for the ps_apiresources module
49+
50+
```bash
51+
cd modules
52+
rm -fR ps_apiresources
53+
git clone [email protected]:PrestaShop/ps_apiresources.git
54+
cd ps_apiresources
55+
# You need to fork the repository so you can add your custom fork remote, you will push your branch on it to create the PR
56+
git remote add fork git clone [email protected]:{myfork}/ps_apiresources.git
57+
git fetch fork
58+
59+
# Go back to the root and clear the cache
60+
cd ../..
61+
make cc
62+
```
63+
64+
### Create an API Client so you can use the Admin API
65+
66+
```bash
67+
make docker-sh # Open a shell in the docker
68+
./bin/console prestashop:api-client create test --all-scopes --name='Test client' --description='Test client with all scopes' --timeout=3600 --secret=60b3045648285513cae71350b697dce3
69+
# You can now exit from docker shell
70+
```
71+
72+
{{% notice warning %}}
73+
**Always use auto generated secret**
74+
75+
The secret provided here should only be used for development, it is suggested here for convenience so developers have a common value when they discuss, but **this should never be done in production**.
76+
77+
You should not pick your secret yourself and should let PrestaShop generate it automatically for you (the CLI option is optional, remove it and the secret is autogenerated).
78+
{{% /notice %}}
79+
80+
{{% notice warning %}}
81+
**Never use all scopes**
82+
83+
The security of your Admin API rely on having limited API Clients with limited access, only give your Client the bare minimum scopes they need for their usage.
84+
85+
Each integration with an external service should rely on a dedicated API Client that has its dedicated scopes useful for this integration only.
86+
{{% /notice %}}
87+
88+
Go to admin http://localhost:8001/admin-dev/configure/advanced/admin-api (login and ignore the token protection)
89+
90+
You should see a Test client in the list, edit it to check it has all the scopes authorized
91+
92+
![API Client list](../img/api-client-list.png)
93+
94+
### Configure admin API for development
95+
96+
By default, the Admin API **must** be used with HTTPs protocol, in development it's not required though but you need to disable this protection.
97+
98+
{{% notice warning %}}
99+
This should only be done for development locally, never use this in production. It won't work anyway because the safety is forced all the time when you disable the debug mode of PrestaShop.
100+
{{% /notice %}}
101+
102+
In the configuration uncheck the "Force security in debug mode" and save the configuration
103+
104+
![Admin API Debug security option](../img/debug-security-option.png)
105+
106+
### Use swagger as your client
107+
108+
Now go to Swagger, there is a link in the Admin API index in the BO
109+
110+
![Admin API Debug security option](../img/swagger-link.png)
111+
112+
(or go to http://localhost:8001/admin-dev/configure/advanced/admin-api/docs.html)
113+
114+
Click on Authorize, and use the credentials of your Api Client
115+
- client_id: test
116+
- client_secret: 60b3045648285513cae71350b697dce3
117+
- scopes: select all scopes if you don't want to bother and test any endpoints, however when testing your new endpoints you should select **only the scope you need** to make sure they are correctly setup
118+
119+
![Admin API Debug security option](../img/swagger-login.png)
120+
121+
Click on Authorize button at the bottom
122+
123+
You can now use Swagger as your client to call the Admin API, you can also use Postman or similar tools if you prefer.

0 commit comments

Comments
 (0)