Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 49f6b593 authored by Felix's avatar Felix
Browse files

Merge branch 'faster-create' into 'master'

Faster account creation by moving Nextcloud API call to background

See merge request !3
parents b28c7b28 f1280a96
Loading
Loading
Loading
Loading
Loading
+14 −12
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ import logging
import sys
import json
import shlex
from threading import Thread

ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
@@ -72,6 +73,19 @@ class MyHandler(BaseHTTPRequestHandler):
                self.respond(500, data)
                return

        self.respond(200, json.dumps({'success': True}))

        # Run Nextcloud API call in seperate thread. Needed because the API calls take 50+ seconds
        # each for unknown reasons. Instead we just run them in the background.
        Thread(target=self.call_nextcloud_api, args=(target_email, password, fallback_email, nextcloud_quota)).start()

    def respond(self, status: int, message: str):
        print(f'sending response status {status}, message {message}')
        self.send_response(status)
        self.end_headers()
        self.wfile.write(message.encode())

    def call_nextcloud_api(self, target_email: str, password: str, fallback_email: str, nextcloud_quota: str):
        # Nextcloud only creates external accounts after the first login. We login through the API
        # to trigger the account creation.
        headers = {'OCS-APIRequest': 'true'}
@@ -88,18 +102,6 @@ class MyHandler(BaseHTTPRequestHandler):
        print(f'status code: {r1.status_code}')
        print(r2.text)
        print(f'status code: {r2.status_code}')
        if r1.status_code != 200 or r2.status_code != 200:
            data = json.dumps({'success': False, 'message': 'internal_error'})
            self.respond(403, data)
            return

        self.respond(200, json.dumps({'success': True}))

    def respond(self, status: int, message: str):
        print(f'sending response status {status}, message {message}')
        self.send_response(status)
        self.end_headers()
        self.wfile.write(message.encode())


if __name__ == '__main__':