FEATURE: Add basic auth support (#200)

FEATURE: Add basic auth support (#200)

Usage:

client.basic_auth = {
  user: "test",
  password: "secret"
}
diff --git a/README.md b/README.md
index 13104a0..34e3552 100644
--- a/README.md
+++ b/README.md
@@ -80,6 +80,15 @@ end
 
 Check out [lib/discourse_api/error.rb](lib/discourse_api/error.rb) and [lib/discourse_api/client.rb](lib/discourse_api/client.rb)'s `handle_error` method for the types of errors raised by the API.
 
+If your forum has a basic HTTP authentication enabled, set user and password:
+
+`‍``ruby
+client.basic_auth = {
+  user: "test",
+  password: "secret"
+}
+`‍``
+
 ## Contributing
 
 1. Fork it
diff --git a/lib/discourse_api/client.rb b/lib/discourse_api/client.rb
index 120a202..488cd3a 100644
--- a/lib/discourse_api/client.rb
+++ b/lib/discourse_api/client.rb
@@ -28,6 +28,7 @@ require 'discourse_api/api/site_settings'
 module DiscourseApi
   class Client
     attr_accessor :api_key
+    attr_accessor :basic_auth
     attr_reader :host, :api_username
 
     include DiscourseApi::API::Categories
@@ -118,6 +119,11 @@ module DiscourseApi
         # Convert request params to "www-form-encoded"
         conn.request :url_encoded
 
+        # Allow to interact with forums behind basic HTTP authentication
+        if basic_auth
+          conn.request :basic_auth, basic_auth[:user], basic_auth[:password]
+        end
+
         # Follow redirects
         conn.response :follow_redirects, limit: 5
 

GitHub sha: 6ba89902

This commit appears in #200 which was approved by eviltrout. It was merged by CvX.