Commit 95616af06dd19f51468a940eea0544e4022dc5c6
1 parent
e75db402
Exists in
master
Add basic department api
Showing
3 changed files
with
34 additions
and
3 deletions
Show diff stats
lib/dingtalk/api/base.rb
@@ -2,19 +2,32 @@ module Dingtalk | @@ -2,19 +2,32 @@ module Dingtalk | ||
2 | module Api | 2 | module Api |
3 | class Base | 3 | class Base |
4 | attr_accessor :corp_id | 4 | attr_accessor :corp_id |
5 | + ACCESS_TOKEN = "access_token" | ||
5 | 6 | ||
6 | def initialize(corp_id = nil) | 7 | def initialize(corp_id = nil) |
7 | @corp_id = corp_id | 8 | @corp_id = corp_id |
8 | end | 9 | end |
9 | 10 | ||
11 | + def access_token | ||
12 | + "#{corp_id}_#{ACCESS_TOKEN}" | ||
13 | + end | ||
14 | + | ||
10 | private | 15 | private |
16 | + def default_params | ||
17 | + { access_token: access_token } | ||
18 | + end | ||
19 | + | ||
20 | + def payload(params = {}) | ||
21 | + [ request_url(url), default_params.merge(params).to_json, { content_type: :json } ] | ||
22 | + end | ||
23 | + | ||
11 | def http_get(url, params = {}) | 24 | def http_get(url, params = {}) |
12 | - res = RestClient.get(request_url(url), params.to_json, content_type: :json) | 25 | + res = RestClient.get(payload(params)) |
13 | JSON.parse(res) | 26 | JSON.parse(res) |
14 | end | 27 | end |
15 | 28 | ||
16 | def http_post(url, params = {}) | 29 | def http_post(url, params = {}) |
17 | - res = RestClient.post(request_url(url), params.to_json, content_type: :json) | 30 | + res = RestClient.post(payload(params)) |
18 | JSON.parse(res) | 31 | JSON.parse(res) |
19 | end | 32 | end |
20 | 33 |
lib/dingtalk/api/suite.rb
@@ -29,7 +29,7 @@ module Dingtalk | @@ -29,7 +29,7 @@ module Dingtalk | ||
29 | suite_secret: @suite_secret, | 29 | suite_secret: @suite_secret, |
30 | suite_ticket: suite_ticket | 30 | suite_ticket: suite_ticket |
31 | } | 31 | } |
32 | - http_post(request_url('get_suite_token'), params) | 32 | + http_post('get_suite_token', params) |
33 | # TODO check response values | 33 | # TODO check response values |
34 | redis.set(SUITE_ACCESS_TOKEN, res['suite_access_token']) | 34 | redis.set(SUITE_ACCESS_TOKEN, res['suite_access_token']) |
35 | redis.expire(SUITE_ACCESS_TOKEN, EXPIRATION) | 35 | redis.expire(SUITE_ACCESS_TOKEN, EXPIRATION) |
@@ -41,6 +41,10 @@ module Dingtalk | @@ -41,6 +41,10 @@ module Dingtalk | ||
41 | end | 41 | end |
42 | 42 | ||
43 | private | 43 | private |
44 | + def default_params | ||
45 | + {} | ||
46 | + end | ||
47 | + | ||
44 | def base_url | 48 | def base_url |
45 | 'service' | 49 | 'service' |
46 | end | 50 | end |