 ! Copyright (C) 2020 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
+
 USING: accessors assocs base64 calendar calendar.format
-checksums.hmac checksums.sha combinators formatting http
-http.client json.reader json.writer kernel math.parser
-namespaces sequences ;
+checksums.hmac checksums.sha combinators combinators.smart
+formatting fry http http.client json.reader json.writer kernel
+locals make math.parser namespaces random sequences ;
+
 IN: visionect
 
 SYMBOL: visionect-base-url


<PRIVATE

: visionect-authorization ( verb date path -- auth )
    "%s\n\n\n%s\n%s" sprintf visionect-api-secret get sha-256
    hmac-bytes >base64 visionect-api-key get ":" rot 3append ;

: set-visionect-headers ( request -- request )
    now timestamp>http-string "Date" set-header
    dup [ method>> ] [ "Date" header ] [ url>> path>> ] tri
    visionect-authorization "Authorization" set-header ;

: visionect-request ( request -- data )
    set-visionect-headers http-request nip ;


! ## BACKENDS

<PRIVATE

: choose-boundary ( data -- boundary )
    f swap '[
        drop 16 random-bytes bytes>hex-string
        dup _ subseq?
    ] loop ;

PRIVATE>

:: http-backend ( png-data uuid -- )
    png-data choose-boundary :> boundary
    "multipart/form-data; boundary=\"" boundary "\"" 3append :> content-type
    content-type <post-data>
    [
        "--" % boundary % "\n" %
        "Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\n" %
        "Content-Type: image/png\n" %
        "\n" %
        png-data % "\n" %
        "--" % boundary % "--\n" %
    ] B{ } make >>data
    "/backend/" uuid append visionect-url
    <post-request>
    content-type "content-type" set-header
    set-visionect-headers http-request 2drop ;
