-
Notifications
You must be signed in to change notification settings - Fork 8
/
github.lisp
51 lines (45 loc) · 1.91 KB
/
github.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
;; -*- mode: common-lisp -*-
;; Copyright (c) 2015,2016 The OpenWordNet-PT project
;; This program and the accompanying materials are made available
;; under the terms described in the LICENSE file.
(in-package :cl-wnbrowser)
;;; https://github.com/skypher/cl-oauth/blob/master/src/util/query-string.lisp
(defun query-string->alist (query-string)
;; TODO: doesn't handle leading ?
(check-type query-string string)
(let* ((kv-pairs (remove "" (split-sequence #\& query-string) :test #'equal))
(alist (mapcar (lambda (kv-pair)
(let ((kv (split-sequence #\= kv-pair)))
(cons (first kv) (second kv))))
kv-pairs)))
alist))
(defun get-access-token (code)
(let ((stream (drakma:http-request
*github-access-token-url*
:method :post
:external-format-out :utf-8
:parameters (list
(cons "client_id" *github-client-id*)
(cons "client_secret" *github-client-secret*)
(cons "code" code)
(cons "accept" "json"))
:want-stream nil)))
(cdr (assoc "access_token"
(query-string->alist (flexi-streams:octets-to-string stream))
:test #'equal))))
(defun get-user (access-token)
(let ((stream (drakma:http-request
*github-user-api*
:method :get
:external-format-out :utf-8
:additional-headers (list (cons "Authorization"
(format nil "token ~a" access-token)))
:want-stream t)))
(setf (flexi-streams:flexi-stream-external-format stream) :utf-8)
(let ((obj (yason:parse stream
:object-as :plist
:object-key-fn #'make-keyword)))
(close stream)
obj)))
(defun get-user-login (user)
(getjso "login" user))