Skip to content

Commit

Permalink
Add `twittering-make-http-request-from-uri'.
Browse files Browse the repository at this point in the history
* twittering-mode.el: Add `twittering-make-http-request-from-uri'.
(twittering-make-http-request): accept a string as the argument
`query-parameters', which is treated as an encoded query string.
(twittering-make-http-request-from-uri): new function, which
generates a HTTP request alist from URI.
  • Loading branch information
cvmat committed Sep 26, 2010
1 parent bd5274b commit c0a89a7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Expand Up @@ -54,6 +54,12 @@
(twittering-http-application-headers): do not add the header field
"Content-Length".

* twittering-mode.el: Add `twittering-make-http-request-from-uri'.
(twittering-make-http-request): accept a string as the argument
`query-parameters', which is treated as an encoded query string.
(twittering-make-http-request-from-uri): new function, which
generates a HTTP request alist from URI.

2010-09-19 Tadashi MATSUO <tad@mymail.twin.jp>

* twittering-mode.el: Fix `twittering-push-uri-onto-kill-ring'.
Expand Down
62 changes: 59 additions & 3 deletions twittering-mode.el
Expand Up @@ -4199,7 +4199,10 @@ automatically filled if necessary.
HOST specifies the host.
PORT specifies the port. This must be an integer.
PATH specifies the absolute path in URI (without query string).
QUERY-PARAMTERS is a list of pairs (key . value).
QUERY-PARAMTERS is a string or an alist.
If QUERY-PARAMTERS is a string, it is treated as an encoded query string.
If QUERY-PARAMTERS is an alist, it represents a list of cons pairs of
string, (query-key . query-value).
POST-BODY specifies the post body sent when METHOD equals to \"POST\".
If POST-BODY is nil, no body is posted.
If USE-SSL is non-nil, the request is performed with SSL.
Expand All @@ -4219,7 +4222,10 @@ The result alist includes the following keys, where a key is a symbol.
(default-port (if use-ssl 443 80))
(port (if port port default-port))
(query-string
(when query-parameters
(cond
((stringp query-parameters)
query-parameters)
((consp query-parameters)
(mapconcat (lambda (pair)
(cond
((stringp pair)
Expand All @@ -4232,7 +4238,9 @@ The result alist includes the following keys, where a key is a symbol.
(t
nil)))
query-parameters
"&")))
"&"))
(t
nil)))
(uri (concat scheme "://"
host
(when (and port (not (= port default-port)))
Expand All @@ -4259,6 +4267,54 @@ The result alist includes the following keys, where a key is a symbol.
(header-list . ,header-list)
(post-body . ,post-body))))

(defun twittering-make-http-request-from-uri (method header-list uri &optional post-body)
"Returns an alist specifying a HTTP request.
The result alist has the same form as an alist generated by
`twittering-make-http-request'.

METHOD specifies HTTP method. It must be \"GET\" or \"POST\".
HEADER-LIST is a list of (field-name . field-value) specifying HTTP header
fields. The fields \"Host\" and \"User-Agent\" are automatically filled
if necessary.
URI specifies the URI including query string.
POST-BODY specifies the post body sent when METHOD equals to \"POST\".
If POST-BODY is nil, no body is posted."
(let* ((parts-alist
(let ((parsed-url (url-generic-parse-url uri)))
;; This is required for the difference of url library
;; distributed with Emacs 22 and 23.
(cond
((and (fboundp 'url-p) (url-p parsed-url))
;; Emacs 23 and later.
`((scheme . ,(url-type parsed-url))
(host . ,(url-host parsed-url))
(port . ,(url-portspec parsed-url))
(path . ,(url-filename parsed-url))))
((vectorp parsed-url)
;; Emacs 22.
`((scheme . ,(aref parsed-url 0))
(host . ,(aref parsed-url 3))
(port . ,(aref parsed-url 4))
(path . ,(aref parsed-url 5))))
(t
nil))))
(path (let ((path (cdr (assq 'path parts-alist))))
(if (string-match "\\`\\(.*\\)\\?" path)
(match-string 1 path)
path)))
(query-string (let ((path (cdr (assq 'path parts-alist))))
(if (string-match "\\?\\(.*\\)\\'" path)
(match-string 1 path)
nil))))
(twittering-make-http-request method header-list
(cdr (assq 'host parts-alist))
(cdr (assq 'port parts-alist))
path
query-string
post-body
(string= "https"
(cdr (assq 'scheme parts-alist))))))

(defun twittering-make-connection-info (request &optional additional order table)
"Make an alist specifying the information of connection for REQUEST.
REQUEST must be an alist that has the same keys as that generated by
Expand Down

0 comments on commit c0a89a7

Please sign in to comment.