mirror of
https://github.com/chatmail/core.git
synced 2026-04-23 00:16:34 +03:00
json api for provider overview
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
from .capi import ffi, lib
|
||||
from .cutil import as_dc_charpointer, from_dc_charpointer
|
||||
|
||||
import json
|
||||
|
||||
class ProviderNotFoundError(Exception):
|
||||
"""The provider information was not found."""
|
||||
@@ -16,13 +16,12 @@ class Provider(object):
|
||||
"""
|
||||
|
||||
def __init__(self, domain):
|
||||
provider = ffi.gc(
|
||||
lib.dc_provider_new_from_domain(as_dc_charpointer(domain)),
|
||||
lib.dc_provider_unref,
|
||||
provider = from_dc_charpointer(
|
||||
lib.dc_provider_json_from_domain(as_dc_charpointer(domain))
|
||||
)
|
||||
if provider == ffi.NULL:
|
||||
if provider == "":
|
||||
raise ProviderNotFoundError("Provider not found")
|
||||
self._provider = provider
|
||||
self._provider = json.loads(provider)
|
||||
|
||||
@classmethod
|
||||
def from_email(cls, email):
|
||||
@@ -35,33 +34,30 @@ class Provider(object):
|
||||
@property
|
||||
def overview_page(self):
|
||||
"""URL to the overview page of the provider on providers.delta.chat."""
|
||||
return from_dc_charpointer(
|
||||
lib.dc_provider_get_overview_page(self._provider))
|
||||
return "https://providers.delta.chat/" + self._provider['overview_page']
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""The name of the provider."""
|
||||
return from_dc_charpointer(lib.dc_provider_get_name(self._provider))
|
||||
return self._provider['name']
|
||||
|
||||
@property
|
||||
def markdown(self):
|
||||
"""Content of the information page, formatted as markdown."""
|
||||
return from_dc_charpointer(
|
||||
lib.dc_provider_get_markdown(self._provider))
|
||||
return self._provider['markdown']
|
||||
|
||||
@property
|
||||
def status_date(self):
|
||||
"""The date the provider info was last updated, as a string."""
|
||||
return from_dc_charpointer(
|
||||
lib.dc_provider_get_status_date(self._provider))
|
||||
return self._provider['status']['date']
|
||||
|
||||
@property
|
||||
def status(self):
|
||||
"""The status of the provider information.
|
||||
|
||||
This is one of the
|
||||
:attr:`deltachat.const.DC_PROVIDER_STATUS_OK`,
|
||||
:attr:`deltachat.const.DC_PROVIDER_STATUS_PREPARATION` or
|
||||
:attr:`deltachat.const.DC_PROVIDER_STATUS_BROKEN` constants.
|
||||
This is
|
||||
:attr:`"OK"`,
|
||||
:attr:`"PREPARATION"` or
|
||||
:attr:`"BROKEN"`.
|
||||
"""
|
||||
return lib.dc_provider_get_status(self._provider)
|
||||
return self._provider['status']['state']
|
||||
|
||||
@@ -3,6 +3,7 @@ from deltachat import capi, cutil, const, set_context_callback, clear_context_ca
|
||||
from deltachat.capi import ffi
|
||||
from deltachat.capi import lib
|
||||
from deltachat.account import EventLogger
|
||||
import json
|
||||
|
||||
|
||||
def test_empty_context():
|
||||
@@ -103,19 +104,23 @@ def test_get_special_message_id_returns_empty_message(acfactory):
|
||||
|
||||
|
||||
def test_provider_info():
|
||||
provider = lib.dc_provider_new_from_email(cutil.as_dc_charpointer("ex@example.com"))
|
||||
assert cutil.from_dc_charpointer(
|
||||
lib.dc_provider_get_overview_page(provider)
|
||||
) == "https://providers.delta.chat/example.com"
|
||||
assert cutil.from_dc_charpointer(lib.dc_provider_get_name(provider)) == "Example"
|
||||
assert cutil.from_dc_charpointer(lib.dc_provider_get_markdown(provider)) == "\n..."
|
||||
assert cutil.from_dc_charpointer(lib.dc_provider_get_status_date(provider)) == "2018-09"
|
||||
assert lib.dc_provider_get_status(provider) == const.DC_PROVIDER_STATUS_PREPARATION
|
||||
provider_json = cutil.from_dc_charpointer(
|
||||
lib.dc_provider_json_from_email(cutil.as_dc_charpointer("ex@example.com"))
|
||||
)
|
||||
provider = json.loads(provider_json)
|
||||
|
||||
assert provider['overview_page'] == "example.com"
|
||||
assert provider['name'] == "Example"
|
||||
assert provider['markdown'] == "\n..."
|
||||
assert provider['status']['date'] == "2018-09"
|
||||
assert provider['status']['state'] == "PREPARATION"
|
||||
|
||||
|
||||
def test_provider_info_none():
|
||||
assert lib.dc_provider_new_from_email(cutil.as_dc_charpointer("email@unexistent.no")) == ffi.NULL
|
||||
|
||||
provider_json = cutil.from_dc_charpointer(
|
||||
lib.dc_provider_json_from_email(cutil.as_dc_charpointer("email@unexistent.no"))
|
||||
)
|
||||
assert provider_json == ""
|
||||
|
||||
def test_get_info_closed():
|
||||
ctx = ffi.gc(
|
||||
|
||||
@@ -10,7 +10,7 @@ def test_provider_info_from_email():
|
||||
assert example.name == "Example"
|
||||
assert example.markdown == "\n..."
|
||||
assert example.status_date == "2018-09"
|
||||
assert example.status == const.DC_PROVIDER_STATUS_PREPARATION
|
||||
assert example.status == "PREPARATION"
|
||||
|
||||
|
||||
def test_provider_info_from_domain():
|
||||
@@ -19,7 +19,7 @@ def test_provider_info_from_domain():
|
||||
assert example.name == "Example"
|
||||
assert example.markdown == "\n..."
|
||||
assert example.status_date == "2018-09"
|
||||
assert example.status == const.DC_PROVIDER_STATUS_PREPARATION
|
||||
assert example.status == "PREPARATION"
|
||||
|
||||
|
||||
def test_provider_info_none():
|
||||
|
||||
Reference in New Issue
Block a user