<commit_before>; Copyright 2010 <NAME>
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
;   http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.

(ns http.async.client.request
  "Asynchronous HTTP Client - Clojure - Requesting API"
  {:author "<NAME>"}
  (:refer-clojure :exclude [promise])
  (:require [clojure.contrib.io :as duck])
  (:use [http.async.client status headers util]
        [clojure.stacktrace]
        [clojure.contrib.java-utils :only [as-str]]
        [clojure.contrib.str-utils :only [str-join]])
  (:import (com.ning.http.client AsyncHttpClient AsyncHttpClientConfig$Builder
                                 AsyncHandler Cookie
                                 FluentCaseInsensitiveStringsMap
				 HttpResponseStatus HttpResponseHeaders
				 HttpResponseBodyPart
                                 PerRequestConfig
                                 Realm$RealmBuilder Realm$AuthScheme
                                 Request RequestBuilder
				 RequestType ProxyServer)
           (ahc RequestBuilderWrapper)
           (java.net URLEncoder)
           (java.io InputStream
                    ByteArrayInputStream
                    ByteArrayOutputStream)))

(def *user-agent* "http.async.client/0.2.1-dev")

(def *client*
     (AsyncHttpClient.
      (.build
       (.setUserAgent (AsyncHttpClientConfig$Builder.) *user-agent*))))

(defn- convert-method [method]
  "Converts clj method (:get, :put, ...) to Async Client specific.
   Defaults to :get"
  (cond
   (= method :get) RequestType/GET
   (= method :post) RequestType/POST
   (= method :put) RequestType/PUT
   (= method :delete) RequestType/DELETE
   (= method :head) RequestType/HEAD
   (= method :options) RequestType/OPTIONS
   :default RequestType/GET))

(defn get-encoding
  "Gets content encoding from headers, if Content-Type header not present
  or media-type in it is missing => nil"
  [{ct :content-type}]
  (when-let [match (re-matches #".*charset\s*=\s*(.*)\s*" ct)]
    (.toUpperCase (match 1))))

;; default set of callbacks

;; status callbacks
(defn status-collect [_ status]
  "Returns all status and procides with execution"
  [status :continue])

;; header callbacks
(defn headers-collect [_ headers]
  "Reurns all headers, or aborts if no headers provided."<commit_msg>Follow recent refactoring of AHC.<commit_after>; Copyright 2010 <NAME>
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
;   http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.

(ns http.async.client.request
  "Asynchronous HTTP Client - Clojure - Requesting API"
  {:author "<NAME>"}
  (:refer-clojure :exclude [promise])
  (:require [clojure.contrib.io :as duck])
  (:use [http.async.client status headers util]
        [clojure.stacktrace]
        [clojure.contrib.java-utils :only [as-str]]
        [clojure.contrib.str-utils :only [str-join]])
  (:import (com.ning.http.client AsyncHttpClient AsyncHttpClientConfig$Builder
                                 AsyncHandler Cookie
                                 FluentCaseInsensitiveStringsMap
				 HttpResponseStatus HttpResponseHeaders
				 HttpResponseBodyPart
                                 PerRequestConfig
                                 Realm$RealmBuilder Realm$AuthScheme
                                 Request RequestBuilder
                                 ProxyServer)
           (ahc RequestBuilderWrapper)
           (java.net URLEncoder)
           (java.io InputStream
                    ByteArrayInputStream
                    ByteArrayOutputStream)))

(def *user-agent* "http.async.client/0.2.1-dev")

(def *client*
     (AsyncHttpClient.
      (.build
       (.setUserAgent (AsyncHttpClientConfig$Builder.) *user-agent*))))

(defn- convert-method [method]
  "Converts clj method (:get, :put, ...) to Async Client specific.
   Defaults to :get"
  (cond
   (= method :get) "GET"
   (= method :post) "POST"
   (= method :put) "PUT"
   (= method :delete) "DELETE"
   (= method :head) "HEAD"
   (= method :options) "OPTIONS"
   :default "GET"))

(defn get-encoding
  "Gets content encoding from headers, if Content-Type header not present
  or media-type in it is missing => nil"
  [{ct :content-type}]
  (when-let [match (re-matches #".*charset\s*=\s*(.*)\s*" ct)]
    (.toUpperCase (match 1))))

;; default set of callbacks

;; status callbacks
(defn status-collect [_ status]
  "Returns all status and procides with execution"
  [status :continue])

;; header callbacks
(defn headers-collect [_ headers]
  "Reurns all headers, or aborts if no headers provided."
{'fraction_non_alphanumeric': 0.10367892976588629, 'fraction_numerical': 0.004459308807134894, 'mean_word_length': 2.733009708737864, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1988179', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1484, 'n_words': 488}