Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Merge JS binding module for the API #3

Merged
merged 3 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions bindings/js/swift_x_account_sharing_bind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Swift cross account container sharing API JavaScript bindings module.

function parse_list_string (to_parse) {
// Parse the JS list into comma separated values in string.
let ret = "";
to_parse.forEach(user => {
ret = ret.concat(user, ",");
});
return ret.slice(0, ret.length - 1);
}

export async function x_account_api_get_access (
username
) {
// List the containers the user has been given access to.
let url = new URL("/access/".concat(username), document.location);
let containers = fetch(
url, {method: "GET"}
).then(
(resp) => {return resp.json();}
);
return containers;
};

export async function x_account_api_get_access_details(
username,
container,
owner
) {
// Get details from a container the user has been given access to.
let url = new URL(
"/access/".concat(username, "/", container), document.location
);
url.searchParams.append("owner", owner);
let details = fetch(
url, {method: "GET"}
).then(
(resp) => {return resp.json();}
);
return details;
};

export async function x_account_api_get_share(
username
) {
// List the containers the user has shared to another user / users.
let url = new URL("/share/".concat(username), document.location);
let shared = fetch(
url, {method: "GET"}
).then(
(resp) => {return resp.json()}
);
return shared;
};

export async function x_account_api_get_share_details(
username,
container
) {
// Get details from a container the user has given access to.
let url = new URL(
"/share/".concat(username, "/", container), document.location
);
let details = fetch(
url, {method: "GET"}
).then(
(resp) => {return resp.json()}
);
return details;
};

export async function x_account_api_share_new_access(
username,
container,
userlist,
accesslist,
address
) {
// Upload details about a new share action.
let url = new URL(
"/share/".concat(username, "/", container), document.location
);
url.searchParams.append("user", parse_list_string(userlist));
url.searchParams.append("access", parse_list_string(accesslist));
url.searchParams.append("address", address);
let shared = fetch(
url, {method: "POST"}
).then(
(resp) => {return resp.json()}
);
return shared;
}

export async function x_account_api_share_edit_access(
username,
container,
userlist,
accesslist
) {
// Edit the details of an existing share action.
let url = new URL(
"/share/".concat(username, "/", container), document.location
);
url.searchParams.append("user", parse_list_string(userlist));
url.searchParams.append("access", parse_list_string(accesslist));
let shared = fetch(
url, {method: "PATCH"}
).then(
(resp) => {return resp.json()}
);
return shared;
}

export async function x_account_api_share_delete_access(
username,
container,
userlist
) {
// Delete the details of an existing share action.
let url = new URL(
"/share/".concat(username, "/", container), document.location
);
url.searchParams.append("user", parse_list_string(userlist));
let deleted = fetch(
url, {method: "DELETE"}
).then(
(resp) => {
if (resp.status == 204) {return true};
if (resp.status == 404) {return false};
}
)
return deleted;
}
52 changes: 48 additions & 4 deletions swift_x_account_sharing/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
"""Sharing backend API specification and implementation."""


import logging


import aiohttp.web


MODULE_LOGGER = logging.getLogger("api")


async def has_access_handler(request):
"""Handle has-access endpoint query."""
# Future authorization check here
Expand All @@ -13,6 +19,12 @@ async def has_access_handler(request):
access_list = request.app["db_conn"].get_access_list(
request.match_info["user"]
)

MODULE_LOGGER.log(
logging.DEBUG,
"Returning following access list: %s", str(access_list)
)

return aiohttp.web.json_response(access_list)


Expand All @@ -27,6 +39,12 @@ async def access_details_handler(request):
request.query["owner"],
request.match_info["container"]
)

MODULE_LOGGER.log(
logging.DEBUG,
"Returning following access details: %s", str(access_details)
)

return aiohttp.web.json_response(access_details)


Expand All @@ -39,6 +57,13 @@ async def gave_access_handler(request):
shared_list = request.app["db_conn"].get_shared_list(
request.match_info["owner"]
)

MODULE_LOGGER.log(
logging.DEBUG,
"Returning following shared container listing: %s",
str(shared_list)
)

return aiohttp.web.json_response(shared_list)


Expand All @@ -52,6 +77,13 @@ async def shared_details_handler(request):
request.match_info["owner"],
request.match_info["container"]
)

MODULE_LOGGER.log(
logging.DEBUG,
"Returning following shared container details: %s",
str(shared_details)
)

return aiohttp.web.json_response(shared_details)


Expand All @@ -61,19 +93,21 @@ async def share_container_handler(request):

# Check for incorrect client query here

request.app["db_conn"].add_share(
shared = request.app["db_conn"].add_share(
request.match_info["owner"],
request.match_info["container"],
request.query["user"].split(","),
request.query["access"].split(","),
request.query["address"]
)

return aiohttp.web.Response(
status=204,
body="OK"
MODULE_LOGGER.log(
logging.DEBUG,
"Added following new shares: %s", str(shared)
)

return aiohttp.web.json_response(shared)


async def edit_share_handler(request):
"""Handle container shared rights editions."""
Expand All @@ -88,6 +122,11 @@ async def edit_share_handler(request):
request.query["access"].split(",")
)

MODULE_LOGGER.log(
logging.DEBUG,
"Edited following shares: %s", str(edited)
)

return aiohttp.web.json_response(edited)


Expand All @@ -103,6 +142,11 @@ async def delete_share_handler(request):
request.query["user"].split(",")
)

MODULE_LOGGER.log(
logging.DEBUG,
"Deleted following shares: %s", str(deleted)
)

if not deleted:
raise aiohttp.web.HTTPNotFound()

Expand Down
15 changes: 12 additions & 3 deletions swift_x_account_sharing/dict_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
# queries into the final database should probably require the same things.


import logging
import json


MODULE_LOGGER = logging.getLogger("dict_db")


class InMemDB():
"""Class emulating an in memory database."""

Expand All @@ -27,19 +31,23 @@ def load_from_file(self, filename):
def add_share(self, owner, container, userlist, access, address):
"""Add a share action to database."""
# Now assuming that there are no duplicates
new_shares = []
for key in userlist:
self.shares.append({
new_share = {
"owner": owner,
"container": container,
"sharedTo": key,
"access": access,
"address": address
})
}
self.shares.append(new_share)
new_shares.append(new_share)
return new_shares

def edit_share(self, owner, container, userlist, access):
"""Edit a share action in the database."""
if not access:
return
return []
new_shares = []
# For now iterate over whole list
for key in userlist:
Expand All @@ -60,6 +68,7 @@ def edit_share(self, owner, container, userlist, access):
if new_shares:
for i in new_shares:
self.shares.append(i)
return new_shares

def delete_share(self, owner, container, userlist):
"""Remove a share action from the database."""
Expand Down
3 changes: 3 additions & 0 deletions swift_x_account_sharing/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
from .dict_db import InMemDB


logging.basicConfig(level=logging.DEBUG)


asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())


Expand Down