clean up lib/oauth1 types

This commit is contained in:
Anton Dyudin 2016-03-08 19:49:58 -08:00
parent 50b8fb028b
commit d3e3190c51
2 changed files with 50 additions and 65 deletions

View File

@ -7,47 +7,54 @@
$: con/{tok/@t sec/@t} :: user key pair
acc/{tok/@t sec/@t} :: app key pair
==
++ join :: between every pair
|= {a/tape b/wall} ^- tape
?~(b b |-(?~(t.b i.b :(weld i.b a $(b t.b)))))
::
:: query string in oauth1 'k1="v1", k2="v2"' form
++ to-header
|= a/quay ^- tape
%+ join ", "
(turn a |=({k/@t v/@t} `tape`~[k '="' v '"'])) :: normalized later
::
:: partial tail:earn for sorting
++ encode-pairs
|= a/quay ^- (list tape)
%+ turn a
|= {k/@t v/@t} ^- tape
:(weld (urle (trip k)) "=" (urle (trip v)))
--
::
::::
::
|= $: med/meth
url/tape
pas/(list tape)
url/purl
quy/quay
key/keys
now/time
eny/@
==
^- @t
=+ non=(scow %uw (shaf %non eny))
=+ tim=(slag 2 (scow %ui (unt now)))
=+ sky=(crip :(weld (urle (trip sec.con.key)) "&" (urle (trip sec.acc.key))))
=+ ^- aut-quy/quay
:~ 'oauth_consumer_key'^tok.con.key
'oauth_nonce'^(scot %uw (shaf %non eny))
'oauth_signature_method'^'HMAC-SHA1'
'oauth_timestamp'^(rsh 3 2 (scot %ui (unt now)))
'oauth_token'^tok.acc.key
'oauth_version'^'1.0'
==
=+ ^= bas
^- tape
=+ ^= hds
=- (reel (sort - aor) |=({p/tape q/tape} :(weld p "&" q)))
%+ weld pas
^- (list tape)
:~ :(weld "oauth_consumer_key=" (trip tok.con.key))
:(weld "oauth_nonce=" non)
:(weld "oauth_signature_method=HMAC-SHA1")
:(weld "oauth_timestamp=" tim)
:(weld "oauth_token=" (trip tok.acc.key))
:(weld "oauth_version=1.0")
==
;: weld
(trip (cuss (trip `@t`med))) "&"
(urle url) "&"
(urle (scag (dec (lent hds)) `tape`hds))
=+ hds=(sort (encode-pairs (weld quy aut-quy)) aor)
%+ join "&"
:~ (trip (cuss (trip `@t`med)))
(urle (earn url))
(urle (join "&" hds))
==
=+ sky=(crip :(weld (urle (trip sec.con.key)) "&" (urle (trip sec.acc.key))))
=+ sig=`tape`(sifo (swap 3 (hmac (swap 3 sky) (crip bas))))
%- crip
;: weld "OAuth "
"oauth_consumer_key=" "\"" (trip tok.con.key) "\", "
"oauth_nonce=" "\"" non "\", "
"oauth_signature=" "\"" (urle sig) "\", "
"oauth_signature_method=\"HMAC-SHA1\", "
"oauth_timestamp=" "\"" tim "\", "
"oauth_token=" "\"" (trip tok.acc.key) "\", "
"oauth_version=1.0"
==
%+ weld "OAuth "
%+ to-header
'oauth_signature'^(crip (urle sig))
aut-quy

View File

@ -5,7 +5,6 @@
/? 314
/- twitter
/+ oauth1
=+ oauth1=oauth1
=+ sur-twit:^twitter :: XX
!:
:::: functions
@ -13,11 +12,8 @@
|%
++ fass :: rewrite path
|= a/path
%- trip
%^ gsub '-' '_'
%+ reel a
|= {p/@t q/@t}
(cat 3 '/' (cat 3 p q))
%+ turn a
|=(b/@t (gsub '-' '_' b))
::
++ gsub :: replace chars
|= {a/@t b/@t t/@t}
@ -47,40 +43,22 @@
[q.i.b $(a t.a, b t.b)]
::
++ valve :: produce request
|= $: med/meth
{rus/tape quy/quay}
|= $: med/?($get $post)
{pax/path quy/quay}
key/keys
est/time
eny/@
==
^- hiss
=+ url="https://api.twitter.com/1.1{rus}.json"
=+ req=|=(a/tape (scan a auri:epur))
=+ ^= help
|= quy/(list {tape tape})
^- (list tape)
%+ turn quy
|= a/{p/tape q/tape}
:(weld (urle p.a) "=" (urle q.a))
=+ tan=(turn quy |=({p/@t q/@t} [(trip p) (trip q)]))
=+ har=(help (turn tan |=(p/{p/tape q/tape} [p.p (urle q.p)])))
=+ hab=(help tan)
=+ lav=(reel har |=({p/tape q/tape} :(weld p "&" q)))
=+ voy=?:(=(0 (lent lav)) ~ (scag (dec (lent lav)) `tape`lav))
=+ vab=(reel hab |=({p/tape q/tape} :(weld p "&" q)))
=+ vur=(crip ?:(=(0 (lent vab)) ~ (scag (dec (lent vab)) `tape`vab)))
=+ ^= head
%- ~(gas by *math)
~! ^twitter
~! oauth1:^twitter
:~ ['authorization' [(oauth1 med url hab key est eny) ~]]
['content-type' ['application/x-www-form-urlencoded' ~]]
==
?: =(%get med)
?~ voy
[(req url) med head ~]
[(req :(weld url "?" voy)) med head ~]
[(req url) med head (some [(met 3 vur) vur])]
=+ url=(scan "https://api.twitter.com/1.1.json" auri:epur) :: base path
=. q.q.url (welp q.q.url pax)
=+ hed=(~(add ja *math) %authorization (oauth1 med url quy key est eny))
?- med
$get [url(r quy) med hed ~]
$post
=. hed (~(add ja hed) %content-type 'application/x-www-form-urlencoded')
[url med hed (some (tact +:(tail:earn quy)))]
==
--
!:
:::: library
@ -157,7 +135,7 @@
++ ui {$user-id p/tid}
++ us {$user-id p/lid}
--
|_ {key/keys est/time eny/@uw}
|_ {key/keys est/time eny/@uv}
++ lutt |=(@ `@t`(rsh 3 2 (scot %ui +<)))
++ llsc
|= (list scr)