Skip to content

Commit 9fe5322

Browse files
authored
API-1821 Implement Sample Files for PYTHON-SDK Functionality Testing (#39)
* API-1821 Implement Sample Files for PYTHON-SDK Functionality Testing * API-1821 Dockerize testing sample * API-1821 Update README with instructions * API-1821 Add details to README * API-1821 add run docker to Makefile * API-1821 fixed example on secret.example.json * API-1821 add comment on example * API-1821 Add more examples to collections
1 parent 9842c17 commit 9fe5322

20 files changed

+502
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,9 @@ ENV/
9292
# OS generated files
9393
.DS_Store
9494
.idea/
95+
96+
# Ignore secret files
97+
secret.json
98+
99+
# Ignore volume folder created by docker
100+
app/

Dockerfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM python:3.9
2+
ENV PYTHONUNBUFFERED 1
3+
4+
RUN apt-get clean && apt-get update
5+
6+
RUN mkdir /app
7+
WORKDIR /app
8+
9+
RUN pip install bynder-sdk
10+
11+
COPY . /app/

Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,16 @@ testdeps:
3636
.PHONY: typehint
3737
typehint:
3838
mypy --ignore-missing-imports --follow-imports=skip $(DISTNAME)
39+
40+
# make executeSdkSample sample-file-name=metaproperties.py
41+
.PHONY: executeSdkSample
42+
executeSdkSample:
43+
docker-compose exec bynder-python-sdk python /app/samples/$(sample-file-name)
44+
45+
.PHONY: run-docker
46+
run-docker:
47+
docker-compose up -d
48+
49+
.PHONY: stop-docker
50+
stop-docker:
51+
docker-compose down

README.md

+46
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,49 @@ packages required and execute the tests for all the clients.
204204
```bash
205205
make test
206206
```
207+
208+
Docker Setup Guide
209+
-----------------
210+
211+
The Docker setup allows you to run your Python scripts inside a Docker container, with dependencies installed and files synchronized. This guide aims to facilitate the development and testing of the SDK.
212+
213+
### Requirements and dependencies
214+
215+
Ensure the following are installed on your machine:
216+
217+
- [Docker](https://www.docker.com/get-started/)
218+
- [docker-compose](https://docs.docker.com/compose/)
219+
220+
### Initial Setup
221+
222+
Create a `secret.json` file by following the example provided in the project. Fill in the necessary settings based on your requirements. If you have a permanent token, only the domain and permanent_token fields need to be specified:
223+
```
224+
{
225+
"domain": "example.bynder.com", # Without the http:// or https://
226+
"permanent_token": "7d09..........."
227+
}
228+
```
229+
230+
With `docker` and `docker-compose` installed, and your `secret.json` file ready, run the following command to initiate the container:
231+
```bash
232+
make run-docker
233+
```
234+
This command initializes a container with the bynder-python-sdk installed and ready for use.
235+
236+
### Executing SDK Samples
237+
238+
You can utilize the `Makefile` commands on your console to run SDK sample scripts. The syntax is as follows:
239+
```bash
240+
make executeSdkSample sample-file-name=file.py
241+
```
242+
All sample files are located in the `./samples` directory.
243+
244+
> :warning: Caution: The sample scripts are provided as examples. It is crucial to review, add and/or modify the commands before execution. The container updates automatically with changes, ensuring a seamless development experience. Always exercise caution when executing scripts.
245+
246+
## Stopping the Docker Container
247+
248+
When you're done with your development or testing, you can stop the Docker container using the following command:
249+
250+
```bash
251+
make stop-docker
252+
```

docker-compose.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: '3'
2+
services:
3+
bynder-python-sdk:
4+
container_name: bynder-python-sdk
5+
build: .
6+
command: sh -c "tail -f /dev/null"
7+
volumes:
8+
- .:/app

example/app.py samples/app.py

File renamed without changes.

samples/brands.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pprint
2+
3+
from client import BynderClientAuthentication
4+
5+
pp = pprint.PrettyPrinter()
6+
7+
auth_instance = BynderClientAuthentication()
8+
bynder_client = auth_instance.get_auth_client()
9+
10+
# Get the asset bank client
11+
asset_bank_client = bynder_client.asset_bank_client
12+
print('\n> Get brands:')
13+
brands = asset_bank_client.brands()
14+
pp.pprint(brands)

samples/campaign.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pprint
2+
3+
from client import BynderClientAuthentication
4+
5+
pp = pprint.PrettyPrinter()
6+
7+
auth_instance = BynderClientAuthentication()
8+
bynder_client = auth_instance.get_auth_client()
9+
10+
# Get the workflow client
11+
workflow_client = bynder_client.workflow_client
12+
print('\n> Get workflow users:')
13+
workflow_users = workflow_client.users()
14+
workflow_user = workflow_users[0]['ID']
15+
pp.pprint(workflow_users)
16+
17+
print('\n> Create new campaign:')
18+
new_campaign = workflow_client.create_campaign(
19+
name='compaign_name',
20+
key='CKEY',
21+
description='campaign_description',
22+
responsible_id=workflow_user
23+
)
24+
pp.pprint(new_campaign)
25+
26+
27+
print('\n> Get campaigns list:')
28+
campaigns = workflow_client.campaigns()
29+
pp.pprint(campaigns)
30+
31+
32+
print('\n> Get campaigns info:')
33+
campaign_id = campaigns[0]['ID']
34+
campaign_info = workflow_client.campaign_info(campaign_id)
35+
pp.pprint(campaign_info)
36+
37+
38+
print('\n> Edit a campaign:')
39+
edited_campaign = workflow_client.edit_campaign(
40+
campaign_id=new_campaign['id'],
41+
name='new_compaign_name',
42+
key='NCKEY',
43+
description='new_compaign_description',
44+
responsible_id=workflow_user
45+
)
46+
pp.pprint(edited_campaign)
47+
48+
49+
print('\n> Delete campaign:')
50+
workflow_client.delete_campaign(
51+
campaign_id=new_campaign['id']
52+
)

samples/client.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import json
2+
3+
from bynder_sdk import BynderClient
4+
5+
6+
class BynderClientAuthentication:
7+
8+
def __init__(self, config_file_path='secret.json'):
9+
with open(config_file_path, 'r') as file:
10+
self.config_data = json.load(file)
11+
12+
def token_saver(token):
13+
""" This function will be called by oauthlib-requests when a new
14+
token is retrieved, either after the initial login or refreshing an
15+
existing token. """
16+
print('New token received:')
17+
print(token)
18+
19+
def get_auth_client(self) -> BynderClient:
20+
# When using Permanent Tokens
21+
if self.config_data.get('permanent_token', None):
22+
return BynderClient(
23+
domain=self.config_data.get('domain', None),
24+
permanent_token=self.config_data.get('permanent_token', None)
25+
)
26+
27+
# When using OAuth2
28+
bynder_client = BynderClient(
29+
**self.config_data,
30+
token_saver=self.token_saver, # optional, defaults to empty lambda
31+
)
32+
# Token object example
33+
# token = {
34+
# "access_token": "...",
35+
# "expires_at": 123456789,
36+
# "expires_in": 3599,
37+
# "id_token": "...",
38+
# "refresh_token": "...",
39+
# "scope": ["offline"],
40+
# "token_type": "bearer"
41+
# }
42+
if self.config_data.get('token', None) is None:
43+
print(bynder_client.get_authorization_url())
44+
45+
code = input('Code: ')
46+
print(bynder_client.fetch_token(code))
47+
48+
return bynder_client

samples/collection.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import pprint
2+
3+
from client import BynderClientAuthentication
4+
5+
pp = pprint.PrettyPrinter()
6+
7+
auth_instance = BynderClientAuthentication()
8+
bynder_client = auth_instance.get_auth_client()
9+
10+
# Get the collections client
11+
collection_client = bynder_client.collection_client
12+
print('\n> Create a new collection:')
13+
new_collection = collection_client.create_collection(
14+
name='testing collection python sdk'
15+
)
16+
pp.pprint(new_collection)
17+
18+
print('\n> Get collections list:')
19+
collections = collection_client.collections(query={'keyword': 'testing collection python sdk'})
20+
collection_id = collections[0]['id']
21+
pp.pprint(collections)
22+
23+
print('\n> Get specific collection info:')
24+
collection = collection_client.collection_info(collection_id)
25+
pp.pprint(collection)
26+
27+
28+
# Get the asset bank client to get media id
29+
asset_bank_client = bynder_client.asset_bank_client
30+
media_list = asset_bank_client.media_list({
31+
'count': True,
32+
'limit': 2,
33+
'type': 'image',
34+
'versions': 1
35+
})
36+
media_id = media_list.get('media')[0].get('id')
37+
38+
print('\n> Add media assets to specific collection:')
39+
collection = collection_client.add_media_to_collection(
40+
collection_id,
41+
media_ids=[media_id]
42+
)
43+
pp.pprint(collection)
44+
45+
print('\n> Get media ids of a collection:')
46+
collection_media_ids = collection_client.collection_media_ids(
47+
collection_id=collection_id
48+
)
49+
pp.pprint(collection_media_ids)
50+
51+
print('\n> Remove media from specific collection:')
52+
collection = collection_client.remove_media_from_collection(
53+
collection_id,
54+
media_ids=[media_id]
55+
)
56+
pp.pprint(collection)
57+
58+
print('\n> Delete a collection:')
59+
deleted_collection = collection_client.delete_collection(
60+
collection_id=collection_id
61+
)
62+
pp.pprint(deleted_collection)
File renamed without changes.

samples/media.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pprint
2+
3+
from client import BynderClientAuthentication
4+
5+
pp = pprint.PrettyPrinter()
6+
7+
auth_instance = BynderClientAuthentication()
8+
bynder_client = auth_instance.get_auth_client()
9+
10+
# Get the asset bank client
11+
asset_bank_client = bynder_client.asset_bank_client
12+
print('\n> Get media list:')
13+
media_list = asset_bank_client.media_list({
14+
'count': True,
15+
'limit': 2,
16+
'type': 'image',
17+
'versions': 1
18+
})
19+
pp.pprint(media_list)
20+
21+
22+
print('\n> Get media info:')
23+
media_id = media_list.get('media')[0].get('id')
24+
media_info = asset_bank_client.media_info(
25+
media_id=media_id,
26+
versions={
27+
'versions': 1
28+
}
29+
)
30+
pp.pprint(media_info)
31+
32+
print('\n Set media description:')
33+
media = asset_bank_client.set_media_properties(
34+
media_id,
35+
{'description': 'Description set using SDK'}
36+
)
37+
38+
print('\n> Get download url:')
39+
download_url = asset_bank_client.media_download_url(
40+
media_id=media_id
41+
)
42+
pp.pprint(download_url)
43+

samples/metaproperties.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pprint
2+
3+
from client import BynderClientAuthentication
4+
5+
pp = pprint.PrettyPrinter()
6+
7+
auth_instance = BynderClientAuthentication()
8+
bynder_client = auth_instance.get_auth_client()
9+
10+
# Get the asset bank client
11+
asset_bank_client = bynder_client.asset_bank_client
12+
print('\n> Get metaproperties:')
13+
meta_properties = asset_bank_client.meta_properties()
14+
pp.pprint(meta_properties)

samples/pim_metaproperties.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pprint
2+
3+
from client import BynderClientAuthentication
4+
5+
pp = pprint.PrettyPrinter()
6+
7+
auth_instance = BynderClientAuthentication()
8+
bynder_client = auth_instance.get_auth_client()
9+
10+
# Get the PIM client
11+
pim_client = bynder_client.pim_client
12+
print('\n> Get list of PIM metaproperties:')
13+
pim_metaproperties = pim_client.metaproperties()
14+
pim_metaproperty_id = pim_metaproperties[0]
15+
pp.pprint(pim_metaproperties)
16+
17+
18+
print('\n> Get metaproperty info:')
19+
pim_metaproperty = pim_client.metaproperty_info(
20+
metaproperty_id=pim_metaproperty_id
21+
)
22+
pp.pprint(pim_metaproperty_id)
23+
24+
25+
print('\n> Get list of PIM metaproperty options:')
26+
pim_metaproperty_options = pim_client.metaproperty_options(
27+
metaproperty_id=pim_metaproperty_id
28+
)
29+
pim_metaproperty_option_id = pim_metaproperty_options[0]['id']
30+
pp.pprint(pim_metaproperty_options)

samples/tags.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pprint
2+
3+
from client import BynderClientAuthentication
4+
5+
pp = pprint.PrettyPrinter()
6+
7+
auth_instance = BynderClientAuthentication()
8+
bynder_client = auth_instance.get_auth_client()
9+
10+
# Get the asset bank client
11+
asset_bank_client = bynder_client.asset_bank_client
12+
print('\n> Get tags:')
13+
tags = asset_bank_client.tags()
14+
pp.pprint(tags)

0 commit comments

Comments
 (0)