1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-04 10:19:16 +02:00

add basic create contact API

This commit is contained in:
holger krekel 2018-09-07 20:01:46 +02:00
parent a244393860
commit b153089d4c
4 changed files with 44 additions and 7 deletions

View file

@ -2,6 +2,7 @@ from __future__ import print_function
import threading import threading
import requests import requests
from . import capi from . import capi
from .capi import ffi
import deltachat import deltachat
@ -36,6 +37,30 @@ class EventHandler:
return 0 # always online return 0 # always online
class Contact:
def __init__(self, dc_context, contact_id):
self.dc_context = dc_context
self.id = contact_id
# XXX do we need to free dc_contact_t? (we own it according to API)
self.dc_contact_t = capi.lib.dc_get_contact(self.dc_context, contact_id)
@property
def addr(self):
return ffi.string(capi.lib.dc_contact_get_addr(self.dc_contact_t))
@property
def display_name(self):
return ffi.string(capi.lib.dc_contact_get_display_name(self.dc_contact_t))
@property
def is_blocked(self):
return capi.lib.dc_contact_is_blocked(self.dc_contact_t)
@property
def is_verified(self):
return capi.lib.dc_contact_is_verified(self.dc_contact_t)
class Account: class Account:
def __init__(self, db_path, logcallback=None, eventhandler=None): def __init__(self, db_path, logcallback=None, eventhandler=None):
self.dc_context = ctx = capi.lib.dc_context_new( self.dc_context = ctx = capi.lib.dc_context_new(
@ -48,6 +73,7 @@ class Account:
if eventhandler is None: if eventhandler is None:
eventhandler = EventHandler(self.dc_context) eventhandler = EventHandler(self.dc_context)
self._eventhandler = eventhandler self._eventhandler = eventhandler
self._threads = IOThreads(self.dc_context)
def set_config(self, **kwargs): def set_config(self, **kwargs):
for name, value in kwargs.items(): for name, value in kwargs.items():
@ -60,10 +86,13 @@ class Account:
res = capi.lib.dc_get_config(self.dc_context, name, b'') res = capi.lib.dc_get_config(self.dc_context, name, b'')
return capi.ffi.string(res).decode("utf8") return capi.ffi.string(res).decode("utf8")
def create_contact(self, emailadr, name=ffi.NULL):
contact_id = capi.lib.dc_create_contact(self.dc_context, name, emailadr)
return Contact(self.dc_context, contact_id)
def start(self): def start(self):
deltachat.set_context_callback(self.dc_context, self.process_event) deltachat.set_context_callback(self.dc_context, self._process_event)
capi.lib.dc_configure(self.dc_context) capi.lib.dc_configure(self.dc_context)
self._threads = IOThreads(self.dc_context)
self._threads.start() self._threads.start()
def shutdown(self): def shutdown(self):
@ -76,7 +105,7 @@ class Account:
# threads might still need it. # threads might still need it.
# capi.lib.dc_close(self.dc_context) # capi.lib.dc_close(self.dc_context)
def process_event(self, ctx, evt_name, data1, data2): def _process_event(self, ctx, evt_name, data1, data2):
assert ctx == self.dc_context assert ctx == self.dc_context
self._logcallback((evt_name, data1, data2)) self._logcallback((evt_name, data1, data2))
method = getattr(self._eventhandler, evt_name.lower(), None) method = getattr(self._eventhandler, evt_name.lower(), None)

View file

@ -30,12 +30,13 @@ def acfactory(pytestconfig, tmpdir, request):
self.configlist.append(d) self.configlist.append(d)
self.count = 0 self.count = 0
def get_live_account(self, logcallback=None): def get_live_account(self, logcallback=None, started=True):
configdict = self.configlist.pop(0) configdict = self.configlist.pop(0)
tmpdb = tmpdir.join("testdb%d" % self.count) tmpdb = tmpdir.join("testdb%d" % self.count)
ac = Account(tmpdb.strpath, logcallback=logcallback) ac = Account(tmpdb.strpath, logcallback=logcallback)
ac.set_config(**configdict) ac.set_config(**configdict)
ac.start() if started:
ac.start()
request.addfinalizer(ac.shutdown) request.addfinalizer(ac.shutdown)
return ac return ac

View file

@ -8,6 +8,15 @@ except ImportError:
class TestLive: class TestLive:
def test_contacts(self, acfactory):
ac1 = acfactory.get_live_account(started=False)
contact1 = ac1.create_contact("some1@hello.com", name="some1")
assert contact1.id
assert contact1.addr == "some1@hello.com"
assert contact1.display_name == "some1"
assert not contact1.is_blocked
assert not contact1.is_verified
def test_basic_configure_login_ok(self, acfactory): def test_basic_configure_login_ok(self, acfactory):
q = Queue() q = Queue()
ac1 = acfactory.get_live_account(logcallback=q.put) ac1 = acfactory.get_live_account(logcallback=q.put)

View file

@ -34,8 +34,6 @@ minversion = 2.0
python_files = tests/test_*.py python_files = tests/test_*.py
norecursedirs = .tox ja .hg cx_freeze_source norecursedirs = .tox ja .hg cx_freeze_source
xfail_strict=true xfail_strict=true
filterwarnings =
# produced by path.local
[flake8] [flake8]
max-line-length = 120 max-line-length = 120