Ejemplo n.º 1
0
def test_OpisenseObject_Calls():
    # POST
    site = oc.OpisenseObject(type='site', opisense_object=opisense_site)
    result = site.POST(opisense_token)
    assert result.status_code == 200
    site.id = result.json()
    assert type(result.json()) == int

    source = oc.OpisenseObject(type='source', opisense_object=opisense_source)
    source.content['siteId'] = site.id
    result = source.POST(opisense_token)
    source.id = result.json()['id']
    assert result.status_code == 200
    assert type(result.json()) == dict

    variable = oc.OpisenseObject(type='variable',
                                 opisense_object=opisense_variable)
    variable.content['sourceId'] = source.id
    result = variable.POST(opisense_token, parent_id=source.id)
    variable.id = result.json()['id']
    assert result.status_code == 200
    assert type(result.json()) == dict

    # PUT
    source.content['name'] = 'Source New Name'
    result = source.PUT(opisense_token)
    assert result.status_code == 204
    assert oc.GET(opisense_token,
                  oc.ApiFilter('sources', id=source.id),
                  json_output=True)[0]['name'] == 'Source New Name'

    variable.content['name'] = 'Variable New Name'
    result = variable.PUT(opisense_token, parent_id=source.id)
    assert result.status_code == 204
    assert oc.GET(opisense_token,
                  oc.ApiFilter('variables', id=variable.id),
                  json_output=True)[0]['name'] == 'Variable New Name'

    # DELETE
    result = variable.DELETE(opisense_token,
                             force_path='/sources/{}/variables/'.format(
                                 variable.content["sourceId"]))
    assert result.status_code == 204

    result = source.DELETE(opisense_token)
    assert result.status_code == 204

    result = site.DELETE(opisense_token)
    assert result.status_code == 204
Ejemplo n.º 2
0
def authorize(user_credentials: dict, api_credentials: dict, feedback=False):
    """
    gets Opisense Token
    :param user_credentials: dict containing 'client_id' , 'client_secret' and 'scope' keys
    :param api_credentials: dict containing 'username' and 'password' keys
    :param feedback: if True, prints HTTP response code in console
    :return: str : Opisense Token
    """
    client_id = api_credentials['client_id']
    client_secret = api_credentials['client_secret']
    scope = api_credentials['scope']
    oauth = OAuth2Session(client=LegacyApplicationClient(client_id=client_id))
    token = oauth.fetch_token(
        token_url='https://identity.opinum.com/connect/token',
        scope=scope,
        username=user_credentials['username'],
        password=user_credentials['password'],
        client_id=client_id,
        client_secret=client_secret,
        auth=False)
    access_token = 'Bearer ' + token['access_token']
    if feedback == True:
        api_filter = oc.ApiFilter('account')
        account = GET(access_token, api_filter).json()
        print('Got a valid token for the account ' + str(account['id']) +
              ' - ' + str(account['name']))
    return access_token
Ejemplo n.º 3
0
def test_ApiFilter():
    filter = oc.ApiFilter('data',
                          granularity='Hour',
                          date_from='2019-01-01T23:00:00',
                          date_to='2019-01-02T23:00:00')

    assert filter.filters['from'] == '2019-01-01T23:00:00'
    assert filter.filters['to'] == '2019-01-02T23:00:00'
    assert filter.filters == {
        'granularity': 'Hour',
        'from': '2019-01-01T23:00:00',
        'to': '2019-01-02T23:00:00'
    }
    assert filter.path == 'data'
Ejemplo n.º 4
0
def test_StandardData_POST():
    variableId = 991929

    now = datetime.datetime.now()
    start = now
    datapoints = oc.DataPoints(now, random.randint(5, 25))
    for _ in range(5):
        now += datetime.timedelta(minutes=5)
        datapoints.__add__(now, random.randint(5, 25))

    data = oc.StandardData(datapoints, variableId=variableId)

    result = data.POST(opisense_token)
    assert result.status_code == 204

    sleep(5)

    result = oc.GET(
        opisense_token,
        oc.ApiFilter('data',
                     variableId=991929,
                     date_from=start.strftime('%Y-%m-%dT%H:%M:%S%z')))
    assert result.status_code == 200
    assert result.json().__len__() == 6
Ejemplo n.º 5
0
def test_GET():
    assert oc.GET(opisense_token, oc.ApiFilter('units')).status_code == 200