Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
esap-userprofile-python-client
Manage
Activity
Members
Labels
Plan
Wiki
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Yan Grange
esap-userprofile-python-client
Commits
f8cda1bf
Commit
f8cda1bf
authored
3 years ago
by
Hugh Dickinson
Browse files
Options
Downloads
Patches
Plain Diff
Functionality to add shopping items to basket from IDA.
parent
2d135a3f
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
shopping_client/shopping_client.py
+59
-3
59 additions, 3 deletions
shopping_client/shopping_client.py
with
59 additions
and
3 deletions
shopping_client/shopping_client.py
+
59
−
3
View file @
f8cda1bf
...
...
@@ -36,9 +36,13 @@ class shopping_client:
self
.
connectors
=
connectors
self
.
basket
=
None
self
.
username
=
None
def
get_basket
(
self
,
convert_to_pandas
:
bool
=
False
,
reload
:
bool
=
False
,
filter_archives
:
bool
=
False
self
,
convert_to_pandas
:
bool
=
False
,
reload
:
bool
=
False
,
filter_archives
:
bool
=
False
,
)
->
Union
[
list
,
pd
.
DataFrame
,
None
]:
"""
Retrieve the shopping basket for a user.
Prompts for access token if one was not supplied to constructor.
...
...
@@ -87,6 +91,56 @@ class shopping_client:
return
self
.
basket
def
add_to_basket
(
self
,
items
:
list
):
"""
Add items to user
'
s shopping basket.
Parameters
----------
items : list
List of objects, each containing the data for a single
shopping item. Objects should be JSON-serializable. Examples
include Python `dict`s or `list`s.
Returns
-------
bool
`True` if items were successfully added otherwise `False`.
"""
url
=
urllib
.
parse
.
urljoin
(
self
.
host
,
shopping_client
.
endpoint
)
if
self
.
username
is
None
:
# Retrieve username
response
=
requests
.
get
(
url
,
headers
=
self
.
_request_header
())
if
response
.
ok
:
self
.
username
=
json
.
loads
(
response
.
content
)[
"
results
"
][
0
][
"
user_name
"
]
else
:
warn
(
f
"
Unable to retrieve username from
{
self
.
host
}
; is your key valid?
"
)
return
False
,
response
# PATCH not working properly need to add existing basket items
payload
=
{
"
shopping_cart
"
:
[
json
.
loads
(
item
[
"
item_data
"
])
for
item
in
self
.
get_basket
()
]
+
items
}
# trailing "/" required for PATCH
url
=
urllib
.
parse
.
urljoin
(
url
,
self
.
username
)
+
"
/
"
response
=
requests
.
patch
(
url
,
json
=
payload
,
headers
=
self
.
_request_header
())
if
response
.
ok
:
print
(
f
"
{
len
(
items
)
}
item
{
'
s
'
if
len
(
items
)
>
1
else
''
}
successfully added.
"
)
return
True
else
:
warn
(
f
"
Unable to add data to basket at
{
self
.
host
}
; is your key valid?
"
)
return
False
,
response
def
_request_header
(
self
):
while
self
.
token
is
None
:
self
.
_get_token
()
...
...
@@ -102,12 +156,14 @@ class shopping_client:
item_data
=
json
.
loads
(
item
[
"
item_data
"
])
for
connector
in
self
.
connectors
:
if
"
archive
"
in
item_data
and
item_data
[
"
archive
"
]
==
connector
.
archive
:
if
(
"
archive
"
in
item_data
and
item_data
[
"
archive
"
]
==
connector
.
archive
):
filtered_items
.
append
(
item
)
return
filtered_items
def
_basket_to_pandas
(
self
):
if
len
(
self
.
connectors
):
converted_basket
=
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment