Skip to content
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
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ parallel = True

[coverage:report]
precision = 2
fail_under = 98.44
fail_under = 98.45
skip_covered = True
show_missing = True
exclude_lines =
Expand Down
1 change: 1 addition & 0 deletions src/corva/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .api import Api
from .api_adapter import InsertResult
from .handlers import scheduled, stream, task
from .logger import CORVA_LOGGER as Logger
from .models.scheduled.scheduled import (
Expand Down
34 changes: 32 additions & 2 deletions src/corva/api_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import functools
import json
import logging
from typing import Callable, List, Optional
from typing import Callable, List, Optional, Sequence

import httpx
import pydantic
import yaml


Expand Down Expand Up @@ -71,6 +72,12 @@ def wrapper(request: httpx.Request, *args, **kwargs):
return wrapper


class InsertResult(pydantic.BaseModel):
inserted_ids: List[str]
failed_count: int
messages: List[str]


class DataApiV1Sdk:
def __init__(self, client: httpx.Client):
self.http = client
Expand Down Expand Up @@ -105,7 +112,7 @@ def get(
fields: comma separated list of fields to return. Example: "_id,data".

Raises:
requests.HTTPError: if request was unsuccessful.
httpx.HTTPStatusError: if request was unsuccessful.

Returns:
Data from dataset.
Expand All @@ -128,6 +135,29 @@ def get(

return data

def insert(
self, provider: str, dataset: str, *, documents: Sequence[dict]
) -> InsertResult:
"""Inserts data using the endpoint POST 'data/{provider}/{dataset}/'.

Args:
provider: company name owning the dataset.
dataset: dataset name.
documents: data to insert.

Raises:
httpx.HTTPStatusError: if request was unsuccessful.

Returns:
Insert result.
"""

response = self.http.post(url=f"data/{provider}/{dataset}/", json=documents)

response.raise_for_status()

return InsertResult.parse_obj(response.json())


class PlatformApiV1Sdk:
def __init__(self, client: httpx.Client):
Expand Down
315 changes: 315 additions & 0 deletions tests/integration/cassettes/TestUserApiSdk.test_insert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
interactions:
- request:
body: '{"well": {"name": "deleteme-python-sdk-autotest-eb81d18c"}}'
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
connection:
- keep-alive
content-length:
- '59'
content-type:
- application/json
user-agent:
- python-httpx/0.22.0
method: POST
uri: null
response:
content: '{"data":{"id":"328567","type":"well","attributes":{"name":"deleteme-python-sdk-autotest-eb81d18c","status":"unknown","state":"planned"},"relationships":{}}}'
headers:
Cache-Control:
- max-age=0, private, must-revalidate
Connection:
- keep-alive
Content-Encoding:
- gzip
Content-Type:
- application/json; charset=utf-8
Date:
- Mon, 23 May 2022 11:39:27 GMT
ETag:
- W/"ec3494f3333667f241a2e1d1b41205ae"
Referrer-Policy:
- strict-origin-when-cross-origin
Server:
- nginx/1.18.0
Transfer-Encoding:
- chunked
Vary:
- Origin
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- SAMEORIGIN
X-Permitted-Cross-Domain-Policies:
- none
X-Rack-CORS:
- miss; no-origin
X-Request-Id:
- 3462393c-fe38-4cd3-aca1-a4d4a7b722ee
X-Runtime:
- '0.105799'
X-XSS-Protection:
- 1; mode=block
http_version: HTTP/1.1
status_code: 200
- request:
body: ''
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
connection:
- keep-alive
user-agent:
- python-httpx/0.22.0
method: GET
uri: null
response:
content: '{"data":{"id":"328567","type":"well","attributes":{"asset_id":53781610},"relationships":{}}}'
headers:
Cache-Control:
- max-age=0, private, must-revalidate
Connection:
- keep-alive
Content-Encoding:
- gzip
Content-Type:
- application/json; charset=utf-8
Date:
- Mon, 23 May 2022 11:39:27 GMT
ETag:
- W/"c412a0007563bca3e870f857bfe4cf1e"
Referrer-Policy:
- strict-origin-when-cross-origin
Server:
- nginx/1.18.0
Transfer-Encoding:
- chunked
Vary:
- Origin
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- SAMEORIGIN
X-Permitted-Cross-Domain-Policies:
- none
X-Rack-CORS:
- miss; no-origin
X-Request-Id:
- 099e239c-b7af-452d-9340-9456471dbcf9
X-Runtime:
- '0.059369'
X-XSS-Protection:
- 1; mode=block
http_version: HTTP/1.1
status_code: 200
- request:
body: ''
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
connection:
- keep-alive
user-agent:
- python-httpx/0.22.0
method: DELETE
uri: null
response:
content: '{"deleted_count":0}'
headers:
Connection:
- keep-alive
Content-Length:
- '19'
Content-Type:
- application/json
Date:
- Mon, 23 May 2022 11:39:27 GMT
server:
- uvicorn
http_version: HTTP/1.1
status_code: 200
- request:
body: ''
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
connection:
- keep-alive
user-agent:
- python-httpx/0.22.0
method: GET
uri: null
response:
content: '[]'
headers:
Connection:
- keep-alive
Content-Length:
- '2'
Content-Type:
- application/json
Date:
- Mon, 23 May 2022 11:39:28 GMT
server:
- uvicorn
http_version: HTTP/1.1
status_code: 200
- request:
body: '[{"asset_id": 53781610, "version": 1, "data": {"k": "v"}, "timestamp":
10}, {"asset_id": 53781610, "version": 1, "data": {"k": "v"}, "timestamp": 11}]'
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
connection:
- keep-alive
content-length:
- '150'
content-type:
- application/json
user-agent:
- python-httpx/0.22.0
x-corva-app:
- python-sdk-autotest-2022-05-23 11:39:26+00:00
method: POST
uri: null
response:
content: '{"inserted_ids":["628b7271d6aff355ccea8b61","628b7271d6aff355ccea8b62"],"failed_count":0,"messages":[]}'
headers:
Connection:
- keep-alive
Content-Length:
- '103'
Content-Type:
- application/json
Date:
- Mon, 23 May 2022 11:39:29 GMT
server:
- uvicorn
http_version: HTTP/1.1
status_code: 200
- request:
body: ''
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
connection:
- keep-alive
user-agent:
- python-httpx/0.22.0
method: GET
uri: null
response:
content: '[{"_id":"628b7271d6aff355ccea8b61","company_id":80,"asset_id":53781610,"version":1,"provider":"big-data-energy","collection":"python-sdk-autotests","data":{"k":"v"},"timestamp":10,"app_key":"big-data-energy.python_sdk_app_for_autotests"},{"_id":"628b7271d6aff355ccea8b62","company_id":80,"asset_id":53781610,"version":1,"provider":"big-data-energy","collection":"python-sdk-autotests","data":{"k":"v"},"timestamp":11,"app_key":"big-data-energy.python_sdk_app_for_autotests"}]'
headers:
Connection:
- keep-alive
Content-Length:
- '475'
Content-Type:
- application/json
Date:
- Mon, 23 May 2022 11:39:29 GMT
server:
- uvicorn
http_version: HTTP/1.1
status_code: 200
- request:
body: ''
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
connection:
- keep-alive
user-agent:
- python-httpx/0.22.0
method: DELETE
uri: null
response:
content: '{"status":"deleted"}'
headers:
Cache-Control:
- max-age=0, private, must-revalidate
Connection:
- keep-alive
Content-Encoding:
- gzip
Content-Type:
- application/json; charset=utf-8
Date:
- Mon, 23 May 2022 11:39:29 GMT
ETag:
- W/"4df20f95e824b2af44a61642d88daaf0"
Referrer-Policy:
- strict-origin-when-cross-origin
Server:
- nginx/1.18.0
Transfer-Encoding:
- chunked
Vary:
- Origin
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- SAMEORIGIN
X-Permitted-Cross-Domain-Policies:
- none
X-Rack-CORS:
- miss; no-origin
X-Request-Id:
- 8424ca9f-13a6-4ac7-9f70-836a3011124f
X-Runtime:
- '0.099032'
X-XSS-Protection:
- 1; mode=block
http_version: HTTP/1.1
status_code: 200
- request:
body: ''
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
connection:
- keep-alive
user-agent:
- python-httpx/0.22.0
method: DELETE
uri: null
response:
content: '{"deleted_count":2}'
headers:
Connection:
- keep-alive
Content-Length:
- '19'
Content-Type:
- application/json
Date:
- Mon, 23 May 2022 11:39:29 GMT
server:
- uvicorn
http_version: HTTP/1.1
status_code: 200
version: 1
Loading