Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
chatbot canifa
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
chatbot canifa
Commits
d21e9eec
Commit
d21e9eec
authored
Jan 27, 2026
by
Vũ Hoàng Anh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update mock API routes and prefixes
parent
49f43a45
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
32 deletions
+33
-32
canifa_api.py
backend/common/canifa_api.py
+30
-31
run.txt
backend/run.txt
+3
-1
No files found.
backend/common/canifa_api.py
View file @
d21e9eec
...
...
@@ -2,14 +2,16 @@
Canifa API Service
Xử lý các logic liên quan đến API của Canifa (Magento)
"""
import
logging
from
typing
import
Any
import
httpx
from
typing
import
Optional
,
Dict
,
Any
logger
=
logging
.
getLogger
(
__name__
)
# URL API Canifa
CANIFA_CUSTOMER_API
=
"https://canifa.com/v1/magento/customer"
CANIFA_CUSTOMER_API
=
"https://
vsf2.
canifa.com/v1/magento/customer"
# GraphQL Query Body giả lập (để lấy User Info)
CANIFA_QUERY_BODY
=
[
...
...
@@ -17,12 +19,13 @@ CANIFA_QUERY_BODY = [
"customer"
:
"customer-custom-query"
,
"metadata"
:
{
"fields"
:
"
\n
customer {
\n
gender
\n
customer_id
\n
phone_number
\n
date_of_birth
\n
default_billing
\n
default_shipping
\n
email
\n
firstname
\n
is_subscribed
\n
lastname
\n
middlename
\n
prefix
\n
suffix
\n
taxvat
\n
addresses {
\n
city
\n
country_code
\n
default_billing
\n
default_shipping
\n
extension_attributes {
\n
attribute_code
\n
value
\n
}
\n
custom_attributes {
\n
attribute_code
\n
value
\n
}
\n
firstname
\n
id
\n
lastname
\n
postcode
\n
prefix
\n
region {
\n
region_code
\n
region_id
\n
region
\n
}
\n
street
\n
suffix
\n
telephone
\n
vat_id
\n
}
\n
is_subscribed
\n
}
\n
"
}
},
{}
},
{},
]
async
def
verify_canifa_token
(
token
:
str
)
->
Optional
[
Dict
[
str
,
Any
]]:
async
def
verify_canifa_token
(
token
:
str
)
->
dict
[
str
,
Any
]
|
None
:
"""
Verify token với API Canifa (Magento).
Dùng token làm cookie `vsf-customer` để gọi API lấy thông tin customer.
...
...
@@ -39,16 +42,12 @@ async def verify_canifa_token(token: str) -> Optional[Dict[str, Any]]:
headers
=
{
"accept"
:
"application/json, text/plain, */*"
,
"content-type"
:
"application/json"
,
"Cookie"
:
f
"vsf-customer={token}"
# Quan trọng: Gửi token dưới dạng Cookie
"Cookie"
:
f
"vsf-customer={token}"
,
# Quan trọng: Gửi token dưới dạng Cookie
}
try
:
async
with
httpx
.
AsyncClient
(
timeout
=
10.0
)
as
client
:
response
=
await
client
.
post
(
CANIFA_CUSTOMER_API
,
json
=
CANIFA_QUERY_BODY
,
headers
=
headers
)
response
=
await
client
.
post
(
CANIFA_CUSTOMER_API
,
json
=
CANIFA_QUERY_BODY
,
headers
=
headers
)
if
response
.
status_code
==
200
:
data
=
response
.
json
()
...
...
@@ -62,7 +61,6 @@ async def verify_canifa_token(token: str) -> Optional[Dict[str, Any]]:
# Nếu Canifa trả list (batch request)
return
data
else
:
logger
.
warning
(
f
"Canifa API Failed: {response.status_code} - {response.text}"
)
return
None
...
...
@@ -70,7 +68,8 @@ async def verify_canifa_token(token: str) -> Optional[Dict[str, Any]]:
logger
.
error
(
f
"Error calling Canifa API: {e}"
)
return
None
async
def
extract_user_id_from_canifa_response
(
data
:
Any
)
->
Optional
[
str
]:
async
def
extract_user_id_from_canifa_response
(
data
:
Any
)
->
str
|
None
:
"""
Bóc customer_id từ response data của Canifa.
"""
...
...
@@ -83,16 +82,16 @@ async def extract_user_id_from_canifa_response(data: Any) -> Optional[str]:
# Format 1: data['customer']
if
isinstance
(
data
,
dict
):
customer
=
data
.
get
(
'customer'
)
or
data
.
get
(
'data'
,
{})
.
get
(
'customer'
)
customer
=
data
.
get
(
"customer"
)
or
data
.
get
(
"data"
,
{})
.
get
(
"customer"
)
# Format 2: data là list (nếu query batch)
elif
isinstance
(
data
,
list
)
and
len
(
data
)
>
0
:
item
=
data
[
0
]
if
isinstance
(
item
,
dict
):
customer
=
item
.
get
(
'result'
,
{})
.
get
(
'customer'
)
or
item
.
get
(
'data'
,
{})
.
get
(
'customer'
)
customer
=
item
.
get
(
"result"
,
{})
.
get
(
"customer"
)
or
item
.
get
(
"data"
,
{})
.
get
(
"customer"
)
if
customer
and
isinstance
(
customer
,
dict
):
user_id
=
customer
.
get
(
'customer_id'
)
or
customer
.
get
(
'id'
)
user_id
=
customer
.
get
(
"customer_id"
)
or
customer
.
get
(
"id"
)
if
user_id
:
return
str
(
user_id
)
...
...
backend/run.txt
View file @
d21e9eec
...
...
@@ -13,3 +13,5 @@ docker restart chatbot-backend && docker logs -f chatbot-backend
docker logs -f chatbot-backend
docker restart chatbot-backend
sudo docker compose -f docker-compose.prod.yml up -d --build
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment