Skip to content

Commit

Permalink
API-1821 Implement Sample Files for PYTHON-SDK Functionality Testing (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
dylanmartins authored Dec 7, 2023
1 parent 9842c17 commit 9fe5322
Show file tree
Hide file tree
Showing 20 changed files with 502 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@ ENV/
# OS generated files
.DS_Store
.idea/

# Ignore secret files
secret.json

# Ignore volume folder created by docker
app/
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.9
ENV PYTHONUNBUFFERED 1

RUN apt-get clean && apt-get update

RUN mkdir /app
WORKDIR /app

RUN pip install bynder-sdk

COPY . /app/
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,16 @@ testdeps:
.PHONY: typehint
typehint:
mypy --ignore-missing-imports --follow-imports=skip $(DISTNAME)

# make executeSdkSample sample-file-name=metaproperties.py
.PHONY: executeSdkSample
executeSdkSample:
docker-compose exec bynder-python-sdk python /app/samples/$(sample-file-name)

.PHONY: run-docker
run-docker:
docker-compose up -d

.PHONY: stop-docker
stop-docker:
docker-compose down
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,49 @@ packages required and execute the tests for all the clients.
```bash
make test
```

Docker Setup Guide
-----------------

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.

### Requirements and dependencies

Ensure the following are installed on your machine:

- [Docker](https://www.docker.com/get-started/)
- [docker-compose](https://docs.docker.com/compose/)

### Initial Setup

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:
```
{
"domain": "example.bynder.com", # Without the http:// or https://
"permanent_token": "7d09..........."
}
```

With `docker` and `docker-compose` installed, and your `secret.json` file ready, run the following command to initiate the container:
```bash
make run-docker
```
This command initializes a container with the bynder-python-sdk installed and ready for use.

### Executing SDK Samples

You can utilize the `Makefile` commands on your console to run SDK sample scripts. The syntax is as follows:
```bash
make executeSdkSample sample-file-name=file.py
```
All sample files are located in the `./samples` directory.

> :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.
## Stopping the Docker Container

When you're done with your development or testing, you can stop the Docker container using the following command:

```bash
make stop-docker
```
8 changes: 8 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3'
services:
bynder-python-sdk:
container_name: bynder-python-sdk
build: .
command: sh -c "tail -f /dev/null"
volumes:
- .:/app
File renamed without changes.
14 changes: 14 additions & 0 deletions samples/brands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pprint

from client import BynderClientAuthentication

pp = pprint.PrettyPrinter()

auth_instance = BynderClientAuthentication()
bynder_client = auth_instance.get_auth_client()

# Get the asset bank client
asset_bank_client = bynder_client.asset_bank_client
print('\n> Get brands:')
brands = asset_bank_client.brands()
pp.pprint(brands)
52 changes: 52 additions & 0 deletions samples/campaign.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pprint

from client import BynderClientAuthentication

pp = pprint.PrettyPrinter()

auth_instance = BynderClientAuthentication()
bynder_client = auth_instance.get_auth_client()

# Get the workflow client
workflow_client = bynder_client.workflow_client
print('\n> Get workflow users:')
workflow_users = workflow_client.users()
workflow_user = workflow_users[0]['ID']
pp.pprint(workflow_users)

print('\n> Create new campaign:')
new_campaign = workflow_client.create_campaign(
name='compaign_name',
key='CKEY',
description='campaign_description',
responsible_id=workflow_user
)
pp.pprint(new_campaign)


print('\n> Get campaigns list:')
campaigns = workflow_client.campaigns()
pp.pprint(campaigns)


print('\n> Get campaigns info:')
campaign_id = campaigns[0]['ID']
campaign_info = workflow_client.campaign_info(campaign_id)
pp.pprint(campaign_info)


print('\n> Edit a campaign:')
edited_campaign = workflow_client.edit_campaign(
campaign_id=new_campaign['id'],
name='new_compaign_name',
key='NCKEY',
description='new_compaign_description',
responsible_id=workflow_user
)
pp.pprint(edited_campaign)


print('\n> Delete campaign:')
workflow_client.delete_campaign(
campaign_id=new_campaign['id']
)
48 changes: 48 additions & 0 deletions samples/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import json

from bynder_sdk import BynderClient


class BynderClientAuthentication:

def __init__(self, config_file_path='secret.json'):
with open(config_file_path, 'r') as file:
self.config_data = json.load(file)

def token_saver(token):
""" This function will be called by oauthlib-requests when a new
token is retrieved, either after the initial login or refreshing an
existing token. """
print('New token received:')
print(token)

def get_auth_client(self) -> BynderClient:
# When using Permanent Tokens
if self.config_data.get('permanent_token', None):
return BynderClient(
domain=self.config_data.get('domain', None),
permanent_token=self.config_data.get('permanent_token', None)
)

# When using OAuth2
bynder_client = BynderClient(
**self.config_data,
token_saver=self.token_saver, # optional, defaults to empty lambda
)
# Token object example
# token = {
# "access_token": "...",
# "expires_at": 123456789,
# "expires_in": 3599,
# "id_token": "...",
# "refresh_token": "...",
# "scope": ["offline"],
# "token_type": "bearer"
# }
if self.config_data.get('token', None) is None:
print(bynder_client.get_authorization_url())

code = input('Code: ')
print(bynder_client.fetch_token(code))

return bynder_client
62 changes: 62 additions & 0 deletions samples/collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pprint

from client import BynderClientAuthentication

pp = pprint.PrettyPrinter()

auth_instance = BynderClientAuthentication()
bynder_client = auth_instance.get_auth_client()

# Get the collections client
collection_client = bynder_client.collection_client
print('\n> Create a new collection:')
new_collection = collection_client.create_collection(
name='testing collection python sdk'
)
pp.pprint(new_collection)

print('\n> Get collections list:')
collections = collection_client.collections(query={'keyword': 'testing collection python sdk'})
collection_id = collections[0]['id']
pp.pprint(collections)

print('\n> Get specific collection info:')
collection = collection_client.collection_info(collection_id)
pp.pprint(collection)


# Get the asset bank client to get media id
asset_bank_client = bynder_client.asset_bank_client
media_list = asset_bank_client.media_list({
'count': True,
'limit': 2,
'type': 'image',
'versions': 1
})
media_id = media_list.get('media')[0].get('id')

print('\n> Add media assets to specific collection:')
collection = collection_client.add_media_to_collection(
collection_id,
media_ids=[media_id]
)
pp.pprint(collection)

print('\n> Get media ids of a collection:')
collection_media_ids = collection_client.collection_media_ids(
collection_id=collection_id
)
pp.pprint(collection_media_ids)

print('\n> Remove media from specific collection:')
collection = collection_client.remove_media_from_collection(
collection_id,
media_ids=[media_id]
)
pp.pprint(collection)

print('\n> Delete a collection:')
deleted_collection = collection_client.delete_collection(
collection_id=collection_id
)
pp.pprint(deleted_collection)
File renamed without changes
43 changes: 43 additions & 0 deletions samples/media.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pprint

from client import BynderClientAuthentication

pp = pprint.PrettyPrinter()

auth_instance = BynderClientAuthentication()
bynder_client = auth_instance.get_auth_client()

# Get the asset bank client
asset_bank_client = bynder_client.asset_bank_client
print('\n> Get media list:')
media_list = asset_bank_client.media_list({
'count': True,
'limit': 2,
'type': 'image',
'versions': 1
})
pp.pprint(media_list)


print('\n> Get media info:')
media_id = media_list.get('media')[0].get('id')
media_info = asset_bank_client.media_info(
media_id=media_id,
versions={
'versions': 1
}
)
pp.pprint(media_info)

print('\n Set media description:')
media = asset_bank_client.set_media_properties(
media_id,
{'description': 'Description set using SDK'}
)

print('\n> Get download url:')
download_url = asset_bank_client.media_download_url(
media_id=media_id
)
pp.pprint(download_url)

14 changes: 14 additions & 0 deletions samples/metaproperties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pprint

from client import BynderClientAuthentication

pp = pprint.PrettyPrinter()

auth_instance = BynderClientAuthentication()
bynder_client = auth_instance.get_auth_client()

# Get the asset bank client
asset_bank_client = bynder_client.asset_bank_client
print('\n> Get metaproperties:')
meta_properties = asset_bank_client.meta_properties()
pp.pprint(meta_properties)
30 changes: 30 additions & 0 deletions samples/pim_metaproperties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pprint

from client import BynderClientAuthentication

pp = pprint.PrettyPrinter()

auth_instance = BynderClientAuthentication()
bynder_client = auth_instance.get_auth_client()

# Get the PIM client
pim_client = bynder_client.pim_client
print('\n> Get list of PIM metaproperties:')
pim_metaproperties = pim_client.metaproperties()
pim_metaproperty_id = pim_metaproperties[0]
pp.pprint(pim_metaproperties)


print('\n> Get metaproperty info:')
pim_metaproperty = pim_client.metaproperty_info(
metaproperty_id=pim_metaproperty_id
)
pp.pprint(pim_metaproperty_id)


print('\n> Get list of PIM metaproperty options:')
pim_metaproperty_options = pim_client.metaproperty_options(
metaproperty_id=pim_metaproperty_id
)
pim_metaproperty_option_id = pim_metaproperty_options[0]['id']
pp.pprint(pim_metaproperty_options)
14 changes: 14 additions & 0 deletions samples/tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pprint

from client import BynderClientAuthentication

pp = pprint.PrettyPrinter()

auth_instance = BynderClientAuthentication()
bynder_client = auth_instance.get_auth_client()

# Get the asset bank client
asset_bank_client = bynder_client.asset_bank_client
print('\n> Get tags:')
tags = asset_bank_client.tags()
pp.pprint(tags)
Loading

0 comments on commit 9fe5322

Please sign in to comment.