Commit 19a7499d authored by Carlos Manso's avatar Carlos Manso
Browse files

initial commit with base functions

parent 46fc68a9
Loading
Loading
Loading
Loading

src/ids/main.py

0 → 100644
+178 −0
Original line number Diff line number Diff line
import requests
from requests.auth import HTTPBasicAuth

connector_a = "https://localhost:8080"
uri = connector_a
user = 'admin'
password = 'password'


def register_resource(title, description):
    data = {
        "title": "DWD Weather Warnings",
        "description": "DWD weather warnings for germany.",
        "keywords": [
            "DWD"
        ],
        "publisher": "https://dwd.com",
        "language": "DE",
        "license": "",
        "sovereign": "https://dwd.com",
        "endpointDocumentation": "",
        "paymentModality": "undefined"
    }
    result = requests.post(uri + "/api/offers", json=data, verify=False, auth=HTTPBasicAuth(user, password))

    print(result)
    print(result.json())
    return result.json()['_links']['self']['href']


def create_catalog(title, description):
    data = {
        "title": "IDS Catalog",
        "description": "This catalog is created from an IDS infomodel catalog."
    }
    result = requests.post(uri + "/api/catalogs", json=data, verify=False, auth=HTTPBasicAuth(user, password))

    print(result)
    print(result.json())
    try:
        return result.json()['_links']['self']['href']
    except KeyError:
        return result.status_code
        

def add_resource_to_catalog(catalog, resource):
    data = [
        resource
    ]
    result = requests.post(catalog + '/offers', json=data, verify=False, auth=HTTPBasicAuth(user, password))
    print(result)
    print(result.json())
    return result.status_code


def create_rule(description=None, value=None):
    data = {
        "title": "Example usage policy",
        "description": "Usage policy provide access applied.",
        "value": "{\n  \"@context\" : {\n    \"ids\" : \"https://w3id.org/idsa/core/\",\n    \"idsc\" : \"https://w3id.org/idsa/code/\"\n  },\n  \"@type\" : \"ids:Permission\",\n  \"@id\" : \"https://w3id.org/idsa/autogen/permission/51f5f7e4-f97f-4f91-bc57-b243714642be\",\n  \"ids:description\" : [ {\n    \"@value\" : \"Usage policy provide access applied\",\n    \"@type\" : \"http://www.w3.org/2001/XMLSchema#string\"\n  } ],\n  \"ids:title\" : [ {\n    \"@value\" : \"Example Usage Policy\",\n    \"@type\" : \"http://www.w3.org/2001/XMLSchema#string\"\n  } ],\n    \"ids:action\" : [ {\n    \"@id\" : \"https://w3id.org/idsa/code/USE\"\n  } ]\n }"
    }
    result = requests.post(uri + "/api/rules", json=data, verify=False, auth=HTTPBasicAuth(user, password))

    print(result)
    print(result.json())
    try:
        return result.json()['_links']['self']['href']
    except KeyError:
        return result.status_code


def create_contract(description=None, value=None):
    data = {
        "title": "Contract",
        "description": "This is an example contract",
        "provider":"https://connectora:8080/",
        "start": "2023-10-22T07:48:37.068Z",
        "end": "2028-10-22T07:48:37.068Z"
    }
    result = requests.post(uri + "/api/contracts", json=data, verify=False, auth=HTTPBasicAuth(user, password))

    print(result)
    print(result.json())
    try:
        return result.json()['_links']['self']['href']
    except KeyError:
        return result.status_code

def add_rule_to_contract(rule):
    data = {
        "title": "Contract",
        "description": "This is an example contract",
        "provider":"https://connectora:8080/",
        "start": "2023-10-22T07:48:37.068Z",
        "end": "2028-10-22T07:48:37.068Z"
    }
    result = requests.post(uri + "/api/rules", json=data, verify=False, auth=HTTPBasicAuth(user, password))

    print(result)
    print(result.json())
    return result.status_code


def create_artifact(artifact):
    data = {
        "title": "Example artifact with weather data",
        "description": "This is an example artifact that contains information about weather data",
        "value": "{\"coord\":{\"lon\":139.01,\"lat\":35.02},\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"clearsky\",\"icon\":\"01n\"}],\"base\":\"stations\",\"main\":{\"temp\":285.514,\"pressure\":1013.75,\"humidity\":100,\"temp_min\":285.514,\"temp_max\":285.514,\"sea_level\":1023.22,\"grnd_level\":1013.75},\"wind\":{\"speed\":5.52,\"deg\":311},\"clouds\":{\"all\":0},\"dt\":1485792967,\"sys\":{\"message\":0.0025,\"country\":\"JP\",\"sunrise\":1485726240,\"sunset\":1485763863},\"id\":1907296,\"name\":\"Tawarano\",\"cod\":200}", 
        "automatedDownload": True
    }
    result = requests.post(uri + "/api/artifacts", json=data, verify=False, auth=HTTPBasicAuth(user, password))

    print(result)
    print(result.json())
    try:
        return result.json()['_links']['self']['href']
    except KeyError:
        return result.status_code


def create_representation(representation):
    data = {
        "title": "Example Representation",
        "description": "", 
        "mediaType": "application/json", 
        "language": "https://w3id.org/idsa/code/EN"
    }
    result = requests.post(uri + "/api/representations", json=data, verify=False, auth=HTTPBasicAuth(user, password))

    print(result)
    print(result.json())
    return result.status_code


def add_artifact_to_representation(artifact):
    data = [artifact]
    result = requests.post(uri + "/api/artifacts", json=data, verify=False, auth=HTTPBasicAuth(user, password))

    print(result)
    print(result.json())
    return result.status_code

def add_representation_to_offer(representation):
    data = [representation]
    result = requests.post(uri + "/api/representations", json=data, verify=False, auth=HTTPBasicAuth(user, password))

    print(result)
    print(result.json())
    return result.status_code

def add_contract_to_offer(contract):
    data = [contract]
    result = requests.post(uri + "/api/contracts", json=data, verify=False, auth=HTTPBasicAuth(user, password))

    print(result)
    print(result.json())
    return result.status_code


if __name__ == "__main__":
    resource = register_resource('asdf', '223')
    print('resource = ' + resource)
    catalog = create_catalog('asdf', '223')
    print('catalog = ' + catalog)
    add_resource_to_catalog(catalog, resource)
    
    rule = create_rule()
    contract = create_contract()
    add_rule_to_contract(rule)


    artifact = create_artifact()
    representation = create_representation()
    add_artifact_to_representation(artifact)

    add_representation_to_offer(representation)
    add_contract_to_offer(contract)