discourse_api 0.10.1 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +21 -21
- data/.travis.yml +5 -7
- data/CHANGELOG.md +99 -85
- data/Gemfile +4 -4
- data/Guardfile +5 -5
- data/LICENSE.txt +22 -22
- data/README.md +73 -73
- data/Rakefile +7 -7
- data/discourse_api.gemspec +35 -33
- data/examples/backups.rb +18 -18
- data/examples/category.rb +21 -21
- data/examples/change_topic_status.rb +33 -0
- data/examples/create_topic.rb +23 -23
- data/examples/create_user.rb +14 -14
- data/examples/dashboard.rb +13 -13
- data/examples/disposable_invite_tokens.rb +18 -18
- data/examples/example.rb +9 -9
- data/examples/groups.rb +25 -25
- data/examples/invite_users.rb +15 -15
- data/examples/post_action.rb +25 -25
- data/examples/search.rb +9 -9
- data/examples/sso.rb +14 -14
- data/examples/topic_lists.rb +15 -15
- data/examples/update_user.rb +22 -22
- data/lib/discourse_api.rb +5 -5
- data/lib/discourse_api/api/api_key.rb +31 -31
- data/lib/discourse_api/api/backups.rb +24 -24
- data/lib/discourse_api/api/badges.rb +28 -28
- data/lib/discourse_api/api/categories.rb +57 -57
- data/lib/discourse_api/api/dashboard.rb +24 -24
- data/lib/discourse_api/api/email.rb +15 -15
- data/lib/discourse_api/api/groups.rb +52 -52
- data/lib/discourse_api/api/invite.rb +17 -17
- data/lib/discourse_api/api/notifications.rb +9 -9
- data/lib/discourse_api/api/params.rb +55 -55
- data/lib/discourse_api/api/posts.rb +41 -42
- data/lib/discourse_api/api/private_messages.rb +10 -10
- data/lib/discourse_api/api/search.rb +19 -19
- data/lib/discourse_api/api/sso.rb +22 -22
- data/lib/discourse_api/api/tags.rb +10 -10
- data/lib/discourse_api/api/topics.rb +78 -65
- data/lib/discourse_api/api/users.rb +85 -85
- data/lib/discourse_api/client.rb +133 -133
- data/lib/discourse_api/error.rb +17 -17
- data/lib/discourse_api/single_sign_on.rb +110 -110
- data/lib/discourse_api/version.rb +3 -3
- data/routes.txt +203 -203
- data/spec/discourse_api/api/api_key_spec.rb +121 -121
- data/spec/discourse_api/api/backups_spec.rb +23 -23
- data/spec/discourse_api/api/badges_spec.rb +40 -40
- data/spec/discourse_api/api/categories_spec.rb +87 -87
- data/spec/discourse_api/api/email_spec.rb +39 -39
- data/spec/discourse_api/api/groups_spec.rb +94 -94
- data/spec/discourse_api/api/notifications_spec.rb +24 -24
- data/spec/discourse_api/api/params_spec.rb +32 -32
- data/spec/discourse_api/api/posts_spec.rb +41 -29
- data/spec/discourse_api/api/private_messages_spec.rb +22 -22
- data/spec/discourse_api/api/search_spec.rb +30 -30
- data/spec/discourse_api/api/sso_spec.rb +16 -16
- data/spec/discourse_api/api/topics_spec.rb +113 -113
- data/spec/discourse_api/api/users_spec.rb +227 -227
- data/spec/discourse_api/client_spec.rb +115 -115
- data/spec/fixtures/api.json +12 -12
- data/spec/fixtures/backups.json +12 -12
- data/spec/fixtures/badges.json +569 -569
- data/spec/fixtures/categories.json +72 -72
- data/spec/fixtures/category_latest_topics.json +92 -92
- data/spec/fixtures/category_topics.json +91 -91
- data/spec/fixtures/email_list_all.json +749 -749
- data/spec/fixtures/email_settings.json +13 -13
- data/spec/fixtures/generate_api_key.json +12 -12
- data/spec/fixtures/generate_master_key.json +7 -7
- data/spec/fixtures/groups.json +18 -18
- data/spec/fixtures/hot.json +113 -113
- data/spec/fixtures/latest.json +115 -115
- data/spec/fixtures/members_0.json +403 -403
- data/spec/fixtures/members_1.json +363 -363
- data/spec/fixtures/new.json +113 -113
- data/spec/fixtures/notifications.json +16 -16
- data/spec/fixtures/post.json +94 -94
- data/spec/fixtures/post_action_users.json +18 -0
- data/spec/fixtures/private_messages.json +69 -69
- data/spec/fixtures/regenerate_api_key.json +7 -7
- data/spec/fixtures/search.json +24 -24
- data/spec/fixtures/topic.json +739 -739
- data/spec/fixtures/topic_invite_user.json +3 -3
- data/spec/fixtures/topics_created_by.json +49 -49
- data/spec/fixtures/update_trust_level.json +37 -37
- data/spec/fixtures/upload_avatar.json +13 -13
- data/spec/fixtures/user.json +65 -65
- data/spec/fixtures/user_activate_success.json +3 -3
- data/spec/fixtures/user_badges.json +170 -170
- data/spec/fixtures/user_create_success.json +5 -5
- data/spec/fixtures/user_grant_admin.json +37 -37
- data/spec/fixtures/user_list.json +583 -583
- data/spec/fixtures/user_log_out_success.json +3 -3
- data/spec/fixtures/user_update_avatar_success.json +3 -3
- data/spec/fixtures/user_update_user.json +3 -3
- data/spec/fixtures/user_update_username.json +4 -4
- data/spec/spec_helper.rb +61 -61
- metadata +31 -28
@@ -1,19 +1,19 @@
|
|
1
|
-
module DiscourseApi
|
2
|
-
module API
|
3
|
-
module Search
|
4
|
-
# Returns search results that match the specified term.
|
5
|
-
#
|
6
|
-
# @param term [String] a search term
|
7
|
-
# @param options [Hash] A customizable set of options
|
8
|
-
# @option options [String] :type_filter Returns results of the specified type.
|
9
|
-
# @return [Array] Return results as an array of Hashes.
|
10
|
-
def search(term, options={})
|
11
|
-
raise ArgumentError.new("#{term} is required but not specified") unless term
|
12
|
-
raise ArgumentError.new("#{term} is required but not specified") unless !term.empty?
|
13
|
-
|
14
|
-
response = get('/search/query', options.merge(term: term))
|
15
|
-
response[:body]
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
1
|
+
module DiscourseApi
|
2
|
+
module API
|
3
|
+
module Search
|
4
|
+
# Returns search results that match the specified term.
|
5
|
+
#
|
6
|
+
# @param term [String] a search term
|
7
|
+
# @param options [Hash] A customizable set of options
|
8
|
+
# @option options [String] :type_filter Returns results of the specified type.
|
9
|
+
# @return [Array] Return results as an array of Hashes.
|
10
|
+
def search(term, options={})
|
11
|
+
raise ArgumentError.new("#{term} is required but not specified") unless term
|
12
|
+
raise ArgumentError.new("#{term} is required but not specified") unless !term.empty?
|
13
|
+
|
14
|
+
response = get('/search/query', options.merge(term: term))
|
15
|
+
response[:body]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,22 +1,22 @@
|
|
1
|
-
module DiscourseApi
|
2
|
-
module API
|
3
|
-
module SSO
|
4
|
-
def sync_sso(params={})
|
5
|
-
sso = DiscourseApi::SingleSignOn.new
|
6
|
-
sso.sso_secret = params[:sso_secret]
|
7
|
-
sso.name = params[:name]
|
8
|
-
sso.username = params[:username]
|
9
|
-
sso.email = params[:email]
|
10
|
-
sso.external_id = params[:external_id]
|
11
|
-
sso.suppress_welcome_message = params[:suppress_welcome_message] === true
|
12
|
-
sso.avatar_url = params[:avatar_url]
|
13
|
-
sso.avatar_force_update = params[:avatar_force_update] === true
|
14
|
-
params.keys.select{|key| key.to_s.start_with?("custom") }.each do |custom_key|
|
15
|
-
sso.custom_fields[custom_key] = params[custom_key]
|
16
|
-
end
|
17
|
-
|
18
|
-
post("/admin/users/sync_sso", sso.payload)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
1
|
+
module DiscourseApi
|
2
|
+
module API
|
3
|
+
module SSO
|
4
|
+
def sync_sso(params={})
|
5
|
+
sso = DiscourseApi::SingleSignOn.new
|
6
|
+
sso.sso_secret = params[:sso_secret]
|
7
|
+
sso.name = params[:name]
|
8
|
+
sso.username = params[:username]
|
9
|
+
sso.email = params[:email]
|
10
|
+
sso.external_id = params[:external_id]
|
11
|
+
sso.suppress_welcome_message = params[:suppress_welcome_message] === true
|
12
|
+
sso.avatar_url = params[:avatar_url]
|
13
|
+
sso.avatar_force_update = params[:avatar_force_update] === true
|
14
|
+
params.keys.select{|key| key.to_s.start_with?("custom") }.each do |custom_key|
|
15
|
+
sso.custom_fields[custom_key] = params[custom_key]
|
16
|
+
end
|
17
|
+
|
18
|
+
post("/admin/users/sync_sso", sso.payload)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
module DiscourseApi
|
2
|
-
module API
|
3
|
-
module Tags
|
4
|
-
def show_tag(tag)
|
5
|
-
response = get("/tags/#{tag}")
|
6
|
-
response[:body]
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
1
|
+
module DiscourseApi
|
2
|
+
module API
|
3
|
+
module Tags
|
4
|
+
def show_tag(tag)
|
5
|
+
response = get("/tags/#{tag}")
|
6
|
+
response[:body]
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -1,65 +1,78 @@
|
|
1
|
-
module DiscourseApi
|
2
|
-
module API
|
3
|
-
module Topics
|
4
|
-
# :category OPTIONAL name of category, not ID
|
5
|
-
# :skip_validations OPTIONAL boolean
|
6
|
-
# :auto_track OPTIONAL boolean
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
.
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
response
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
1
|
+
module DiscourseApi
|
2
|
+
module API
|
3
|
+
module Topics
|
4
|
+
# :category OPTIONAL name of category, not ID
|
5
|
+
# :skip_validations OPTIONAL boolean
|
6
|
+
# :auto_track OPTIONAL boolean
|
7
|
+
# :created_at OPTIONAL seconds since epoch.
|
8
|
+
def create_topic(args={})
|
9
|
+
args = API.params(args)
|
10
|
+
.required(:title, :raw)
|
11
|
+
.optional(:skip_validations, :category, :auto_track, :created_at, :api_username)
|
12
|
+
post("/posts", args.to_h)
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_topic_action(args)
|
16
|
+
args = API.params(args)
|
17
|
+
.required(:id, :post_action_type_id)
|
18
|
+
post("/post_actions", args.to_h.merge(flag_topic: true))
|
19
|
+
end
|
20
|
+
|
21
|
+
# timestamp is seconds past the epoch.
|
22
|
+
def edit_topic_timestamp(topic_id,timestamp)
|
23
|
+
put("/t/#{topic_id}/change-timestamp", { timestamp: timestamp })
|
24
|
+
end
|
25
|
+
|
26
|
+
def latest_topics(params={})
|
27
|
+
response = get('/latest.json', params)
|
28
|
+
response[:body]['topic_list']['topics']
|
29
|
+
end
|
30
|
+
|
31
|
+
def new_topics(params={})
|
32
|
+
response = get("/new.json", params)
|
33
|
+
response[:body]['topic_list']['topics']
|
34
|
+
end
|
35
|
+
|
36
|
+
def rename_topic(topic_id, title)
|
37
|
+
put("/t/#{topic_id}.json", { topic_id: topic_id, title: title })
|
38
|
+
end
|
39
|
+
|
40
|
+
def recategorize_topic(topic_id, category_id)
|
41
|
+
put("/t/#{topic_id}.json", { topic_id: topic_id, category_id: category_id })
|
42
|
+
end
|
43
|
+
|
44
|
+
def change_topic_status(topic_slug, topic_id, params={})
|
45
|
+
params = API.params(params)
|
46
|
+
.required(:status, :enabled)
|
47
|
+
.optional(:api_username)
|
48
|
+
put("/t/#{topic_slug}/#{topic_id}/status", params.to_h)
|
49
|
+
end
|
50
|
+
|
51
|
+
def topic(id, params={})
|
52
|
+
response = get("/t/#{id}.json", params)
|
53
|
+
response[:body]
|
54
|
+
end
|
55
|
+
|
56
|
+
def topics_by(username, params={})
|
57
|
+
response = get("/topics/created-by/#{username}.json", params)
|
58
|
+
response[:body]['topic_list']['topics']
|
59
|
+
end
|
60
|
+
|
61
|
+
def delete_topic(id)
|
62
|
+
delete("/t/#{id}.json")
|
63
|
+
end
|
64
|
+
|
65
|
+
def topic_posts(topic_id, post_ids=[])
|
66
|
+
url = "/t/#{topic_id}/posts.json"
|
67
|
+
if post_ids.count > 0
|
68
|
+
url << '?'
|
69
|
+
post_ids.each do |id|
|
70
|
+
url << "post_ids[]=#{id}&"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
response = get(url)
|
74
|
+
response[:body]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -1,85 +1,85 @@
|
|
1
|
-
module DiscourseApi
|
2
|
-
module API
|
3
|
-
module Users
|
4
|
-
def activate(id)
|
5
|
-
put "/admin/users/#{id}/activate", api_key: api_key, api_username: api_username
|
6
|
-
end
|
7
|
-
|
8
|
-
def user(username, params={})
|
9
|
-
response = get("/users/#{username}.json", params)
|
10
|
-
response[:body]['user']
|
11
|
-
end
|
12
|
-
|
13
|
-
def user_sso(username)
|
14
|
-
response = get("/admin/users/#{username.downcase}")
|
15
|
-
response[:body]['single_sign_on_record']
|
16
|
-
end
|
17
|
-
|
18
|
-
def update_avatar(args)
|
19
|
-
args = API.params(args)
|
20
|
-
.required(:username)
|
21
|
-
.optional(:file, :url)
|
22
|
-
.default(type: 'avatar', synchronous: true)
|
23
|
-
.to_h
|
24
|
-
upload_response = post("/uploads", args)
|
25
|
-
put("/users/#{args[:username]}/preferences/avatar/pick", { upload_id: upload_response['id'] })
|
26
|
-
end
|
27
|
-
|
28
|
-
def update_email(username, email)
|
29
|
-
put("/users/#{username}/preferences/email", { email: email, api_key: api_key })
|
30
|
-
end
|
31
|
-
|
32
|
-
def update_user(username, params={})
|
33
|
-
put("/users/#{username}", params)
|
34
|
-
end
|
35
|
-
|
36
|
-
def update_username(username, new_username)
|
37
|
-
put("/users/#{username}/preferences/username", { new_username: new_username, api_key: api_key })
|
38
|
-
end
|
39
|
-
|
40
|
-
def update_trust_level(args)
|
41
|
-
args = API.params(args)
|
42
|
-
.required(:user_id, :level)
|
43
|
-
.to_h
|
44
|
-
response = put("/admin/users/#{args[:user_id]}/trust_level", args)
|
45
|
-
response[:body]
|
46
|
-
end
|
47
|
-
|
48
|
-
def create_user(args)
|
49
|
-
args = API.params(args)
|
50
|
-
.required(:name, :email, :password, :username)
|
51
|
-
.optional(:active)
|
52
|
-
.to_h
|
53
|
-
post("/users", args)
|
54
|
-
end
|
55
|
-
|
56
|
-
def log_out(id)
|
57
|
-
post("/admin/users/#{id}/log_out")
|
58
|
-
end
|
59
|
-
|
60
|
-
def invite_admin(args={})
|
61
|
-
post("/admin/users/invite_admin", args)
|
62
|
-
end
|
63
|
-
|
64
|
-
def list_users(type)
|
65
|
-
response = get("admin/users/list/#{type}.json")
|
66
|
-
response[:body]
|
67
|
-
end
|
68
|
-
|
69
|
-
def grant_admin(user_id)
|
70
|
-
response = put("admin/users/#{user_id}/grant_admin")
|
71
|
-
response[:body]
|
72
|
-
end
|
73
|
-
|
74
|
-
def revoke_admin(user_id)
|
75
|
-
response = put("admin/users/#{user_id}/revoke_admin")
|
76
|
-
response[:body]
|
77
|
-
end
|
78
|
-
|
79
|
-
def by_external_id(external_id)
|
80
|
-
response = get("/users/by-external/#{external_id}")
|
81
|
-
response[:body]['user']
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
1
|
+
module DiscourseApi
|
2
|
+
module API
|
3
|
+
module Users
|
4
|
+
def activate(id)
|
5
|
+
put "/admin/users/#{id}/activate", api_key: api_key, api_username: api_username
|
6
|
+
end
|
7
|
+
|
8
|
+
def user(username, params={})
|
9
|
+
response = get("/users/#{username}.json", params)
|
10
|
+
response[:body]['user']
|
11
|
+
end
|
12
|
+
|
13
|
+
def user_sso(username)
|
14
|
+
response = get("/admin/users/#{username.downcase}")
|
15
|
+
response[:body]['single_sign_on_record']
|
16
|
+
end
|
17
|
+
|
18
|
+
def update_avatar(args)
|
19
|
+
args = API.params(args)
|
20
|
+
.required(:username)
|
21
|
+
.optional(:file, :url)
|
22
|
+
.default(type: 'avatar', synchronous: true)
|
23
|
+
.to_h
|
24
|
+
upload_response = post("/uploads", args)
|
25
|
+
put("/users/#{args[:username]}/preferences/avatar/pick", { upload_id: upload_response['id'] })
|
26
|
+
end
|
27
|
+
|
28
|
+
def update_email(username, email)
|
29
|
+
put("/users/#{username}/preferences/email", { email: email, api_key: api_key })
|
30
|
+
end
|
31
|
+
|
32
|
+
def update_user(username, params={})
|
33
|
+
put("/users/#{username}", params)
|
34
|
+
end
|
35
|
+
|
36
|
+
def update_username(username, new_username)
|
37
|
+
put("/users/#{username}/preferences/username", { new_username: new_username, api_key: api_key })
|
38
|
+
end
|
39
|
+
|
40
|
+
def update_trust_level(args)
|
41
|
+
args = API.params(args)
|
42
|
+
.required(:user_id, :level)
|
43
|
+
.to_h
|
44
|
+
response = put("/admin/users/#{args[:user_id]}/trust_level", args)
|
45
|
+
response[:body]
|
46
|
+
end
|
47
|
+
|
48
|
+
def create_user(args)
|
49
|
+
args = API.params(args)
|
50
|
+
.required(:name, :email, :password, :username)
|
51
|
+
.optional(:active, :staged)
|
52
|
+
.to_h
|
53
|
+
post("/users", args)
|
54
|
+
end
|
55
|
+
|
56
|
+
def log_out(id)
|
57
|
+
post("/admin/users/#{id}/log_out")
|
58
|
+
end
|
59
|
+
|
60
|
+
def invite_admin(args={})
|
61
|
+
post("/admin/users/invite_admin", args)
|
62
|
+
end
|
63
|
+
|
64
|
+
def list_users(type)
|
65
|
+
response = get("admin/users/list/#{type}.json")
|
66
|
+
response[:body]
|
67
|
+
end
|
68
|
+
|
69
|
+
def grant_admin(user_id)
|
70
|
+
response = put("admin/users/#{user_id}/grant_admin")
|
71
|
+
response[:body]
|
72
|
+
end
|
73
|
+
|
74
|
+
def revoke_admin(user_id)
|
75
|
+
response = put("admin/users/#{user_id}/revoke_admin")
|
76
|
+
response[:body]
|
77
|
+
end
|
78
|
+
|
79
|
+
def by_external_id(external_id)
|
80
|
+
response = get("/users/by-external/#{external_id}")
|
81
|
+
response[:body]['user']
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/lib/discourse_api/client.rb
CHANGED
@@ -1,133 +1,133 @@
|
|
1
|
-
require 'faraday'
|
2
|
-
require 'faraday_middleware'
|
3
|
-
require 'json'
|
4
|
-
require 'discourse_api/version'
|
5
|
-
require 'discourse_api/api/categories'
|
6
|
-
require 'discourse_api/api/search'
|
7
|
-
require 'discourse_api/api/sso'
|
8
|
-
require 'discourse_api/api/tags'
|
9
|
-
require 'discourse_api/api/topics'
|
10
|
-
require 'discourse_api/api/posts'
|
11
|
-
require 'discourse_api/api/users'
|
12
|
-
require 'discourse_api/api/groups'
|
13
|
-
require 'discourse_api/api/invite'
|
14
|
-
require 'discourse_api/api/private_messages'
|
15
|
-
require 'discourse_api/api/notifications'
|
16
|
-
require 'discourse_api/api/badges'
|
17
|
-
require 'discourse_api/api/email'
|
18
|
-
require 'discourse_api/api/api_key'
|
19
|
-
require 'discourse_api/api/backups'
|
20
|
-
require 'discourse_api/api/dashboard'
|
21
|
-
|
22
|
-
module DiscourseApi
|
23
|
-
class Client
|
24
|
-
attr_accessor :api_key, :api_username
|
25
|
-
attr_reader :host
|
26
|
-
|
27
|
-
include DiscourseApi::API::Categories
|
28
|
-
include DiscourseApi::API::Search
|
29
|
-
include DiscourseApi::API::SSO
|
30
|
-
include DiscourseApi::API::Tags
|
31
|
-
include DiscourseApi::API::Topics
|
32
|
-
include DiscourseApi::API::Posts
|
33
|
-
include DiscourseApi::API::Users
|
34
|
-
include DiscourseApi::API::Groups
|
35
|
-
include DiscourseApi::API::Invite
|
36
|
-
include DiscourseApi::API::PrivateMessages
|
37
|
-
include DiscourseApi::API::Notifications
|
38
|
-
include DiscourseApi::API::Badges
|
39
|
-
include DiscourseApi::API::Email
|
40
|
-
include DiscourseApi::API::ApiKey
|
41
|
-
include DiscourseApi::API::Backups
|
42
|
-
include DiscourseApi::API::Dashboard
|
43
|
-
|
44
|
-
def initialize(host, api_key = nil, api_username = nil)
|
45
|
-
raise ArgumentError, 'host needs to be defined' if host.nil? || host.empty?
|
46
|
-
@host = host
|
47
|
-
@api_key = api_key
|
48
|
-
@api_username = api_username
|
49
|
-
end
|
50
|
-
|
51
|
-
def connection_options
|
52
|
-
@connection_options ||= {
|
53
|
-
url: @host,
|
54
|
-
headers: {
|
55
|
-
accept: 'application/json',
|
56
|
-
user_agent: user_agent,
|
57
|
-
}
|
58
|
-
}
|
59
|
-
end
|
60
|
-
|
61
|
-
def ssl(options)
|
62
|
-
connection_options[:ssl] = options
|
63
|
-
end
|
64
|
-
|
65
|
-
def delete(path, params={})
|
66
|
-
request(:delete, path, params)
|
67
|
-
end
|
68
|
-
|
69
|
-
def get(path, params={})
|
70
|
-
request(:get, path, params)
|
71
|
-
end
|
72
|
-
|
73
|
-
def post(path, params={})
|
74
|
-
response = request(:post, path, params)
|
75
|
-
case response.status
|
76
|
-
when 200
|
77
|
-
response.body
|
78
|
-
else
|
79
|
-
raise DiscourseApi::Error, response.body
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def put(path, params={})
|
84
|
-
request(:put, path, params)
|
85
|
-
end
|
86
|
-
|
87
|
-
def patch(path, params={})
|
88
|
-
request(:patch, path, params)
|
89
|
-
end
|
90
|
-
|
91
|
-
def user_agent
|
92
|
-
@user_agent ||= "DiscourseAPI Ruby Gem #{DiscourseApi::VERSION}"
|
93
|
-
end
|
94
|
-
|
95
|
-
private
|
96
|
-
|
97
|
-
def connection
|
98
|
-
@connection ||= Faraday.new connection_options do |conn|
|
99
|
-
# Follow redirects
|
100
|
-
conn.use FaradayMiddleware::FollowRedirects, limit: 5
|
101
|
-
# Convert request params to "www-form-encoded"
|
102
|
-
conn.request :url_encoded
|
103
|
-
# Parse responses as JSON
|
104
|
-
conn.use FaradayMiddleware::ParseJson, content_type: 'application/json'
|
105
|
-
# Use Faraday's default HTTP adapter
|
106
|
-
conn.adapter Faraday.default_adapter
|
107
|
-
#pass api_key and api_username on every request
|
108
|
-
unless api_username.nil?
|
109
|
-
conn.params['api_key'] = api_key
|
110
|
-
conn.params['api_username'] = api_username
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
def request(method, path, params={})
|
116
|
-
unless Hash === params
|
117
|
-
params = params.to_h if params.respond_to? :to_h
|
118
|
-
end
|
119
|
-
response = connection.send(method.to_sym, path, params)
|
120
|
-
handle_error(response)
|
121
|
-
response.env
|
122
|
-
rescue Faraday::Error::ClientError, JSON::ParserError
|
123
|
-
raise DiscourseApi::Error
|
124
|
-
end
|
125
|
-
|
126
|
-
def handle_error(response)
|
127
|
-
case response.status
|
128
|
-
when 403
|
129
|
-
raise DiscourseApi::UnauthenticatedError.new(response.env[:body])
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
require 'json'
|
4
|
+
require 'discourse_api/version'
|
5
|
+
require 'discourse_api/api/categories'
|
6
|
+
require 'discourse_api/api/search'
|
7
|
+
require 'discourse_api/api/sso'
|
8
|
+
require 'discourse_api/api/tags'
|
9
|
+
require 'discourse_api/api/topics'
|
10
|
+
require 'discourse_api/api/posts'
|
11
|
+
require 'discourse_api/api/users'
|
12
|
+
require 'discourse_api/api/groups'
|
13
|
+
require 'discourse_api/api/invite'
|
14
|
+
require 'discourse_api/api/private_messages'
|
15
|
+
require 'discourse_api/api/notifications'
|
16
|
+
require 'discourse_api/api/badges'
|
17
|
+
require 'discourse_api/api/email'
|
18
|
+
require 'discourse_api/api/api_key'
|
19
|
+
require 'discourse_api/api/backups'
|
20
|
+
require 'discourse_api/api/dashboard'
|
21
|
+
|
22
|
+
module DiscourseApi
|
23
|
+
class Client
|
24
|
+
attr_accessor :api_key, :api_username
|
25
|
+
attr_reader :host
|
26
|
+
|
27
|
+
include DiscourseApi::API::Categories
|
28
|
+
include DiscourseApi::API::Search
|
29
|
+
include DiscourseApi::API::SSO
|
30
|
+
include DiscourseApi::API::Tags
|
31
|
+
include DiscourseApi::API::Topics
|
32
|
+
include DiscourseApi::API::Posts
|
33
|
+
include DiscourseApi::API::Users
|
34
|
+
include DiscourseApi::API::Groups
|
35
|
+
include DiscourseApi::API::Invite
|
36
|
+
include DiscourseApi::API::PrivateMessages
|
37
|
+
include DiscourseApi::API::Notifications
|
38
|
+
include DiscourseApi::API::Badges
|
39
|
+
include DiscourseApi::API::Email
|
40
|
+
include DiscourseApi::API::ApiKey
|
41
|
+
include DiscourseApi::API::Backups
|
42
|
+
include DiscourseApi::API::Dashboard
|
43
|
+
|
44
|
+
def initialize(host, api_key = nil, api_username = nil)
|
45
|
+
raise ArgumentError, 'host needs to be defined' if host.nil? || host.empty?
|
46
|
+
@host = host
|
47
|
+
@api_key = api_key
|
48
|
+
@api_username = api_username
|
49
|
+
end
|
50
|
+
|
51
|
+
def connection_options
|
52
|
+
@connection_options ||= {
|
53
|
+
url: @host,
|
54
|
+
headers: {
|
55
|
+
accept: 'application/json',
|
56
|
+
user_agent: user_agent,
|
57
|
+
}
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
def ssl(options)
|
62
|
+
connection_options[:ssl] = options
|
63
|
+
end
|
64
|
+
|
65
|
+
def delete(path, params={})
|
66
|
+
request(:delete, path, params)
|
67
|
+
end
|
68
|
+
|
69
|
+
def get(path, params={})
|
70
|
+
request(:get, path, params)
|
71
|
+
end
|
72
|
+
|
73
|
+
def post(path, params={})
|
74
|
+
response = request(:post, path, params)
|
75
|
+
case response.status
|
76
|
+
when 200
|
77
|
+
response.body
|
78
|
+
else
|
79
|
+
raise DiscourseApi::Error, response.body
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def put(path, params={})
|
84
|
+
request(:put, path, params)
|
85
|
+
end
|
86
|
+
|
87
|
+
def patch(path, params={})
|
88
|
+
request(:patch, path, params)
|
89
|
+
end
|
90
|
+
|
91
|
+
def user_agent
|
92
|
+
@user_agent ||= "DiscourseAPI Ruby Gem #{DiscourseApi::VERSION}"
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def connection
|
98
|
+
@connection ||= Faraday.new connection_options do |conn|
|
99
|
+
# Follow redirects
|
100
|
+
conn.use FaradayMiddleware::FollowRedirects, limit: 5
|
101
|
+
# Convert request params to "www-form-encoded"
|
102
|
+
conn.request :url_encoded
|
103
|
+
# Parse responses as JSON
|
104
|
+
conn.use FaradayMiddleware::ParseJson, content_type: 'application/json'
|
105
|
+
# Use Faraday's default HTTP adapter
|
106
|
+
conn.adapter Faraday.default_adapter
|
107
|
+
#pass api_key and api_username on every request
|
108
|
+
unless api_username.nil?
|
109
|
+
conn.params['api_key'] = api_key
|
110
|
+
conn.params['api_username'] = api_username
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def request(method, path, params={})
|
116
|
+
unless Hash === params
|
117
|
+
params = params.to_h if params.respond_to? :to_h
|
118
|
+
end
|
119
|
+
response = connection.send(method.to_sym, path, params)
|
120
|
+
handle_error(response)
|
121
|
+
response.env
|
122
|
+
rescue Faraday::Error::ClientError, JSON::ParserError
|
123
|
+
raise DiscourseApi::Error
|
124
|
+
end
|
125
|
+
|
126
|
+
def handle_error(response)
|
127
|
+
case response.status
|
128
|
+
when 403
|
129
|
+
raise DiscourseApi::UnauthenticatedError.new(response.env[:body])
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|