ps_utilities 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ps_utilities/connection.rb +35 -31
- data/lib/ps_utilities/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 637da17d1d419b00baea629e618529517c09fa7e16f71dd358a850b0978fc599
|
4
|
+
data.tar.gz: a1ec6ced4f33ac5ff849dd48c0f216133e7fe291b1224c8dc082d42c16a66876
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 062a02dd9e855ad0533da5eb6c735021a0607ea6a4db59d33f063b223d0c3918e99071760bb456b9316f81b58e5daf1e06b977c92fbe02b0aa035a81d2841244
|
7
|
+
data.tar.gz: cca916c4339884397ebf5cc40fd6dcd1f8f0ff6d6a17b3cafdf61f30fccf0584ecab4f6bec7b58c7dc3d476b6168810155780dd3a61832861f9958519f2b3690
|
@@ -24,7 +24,7 @@ module PsUtilities
|
|
24
24
|
# @note You should use environment variables to initialize your server.
|
25
25
|
class Connection
|
26
26
|
|
27
|
-
attr_reader :credentials, :headers
|
27
|
+
attr_reader :credentials, :headers, :base_uri, :auth_path, :version
|
28
28
|
|
29
29
|
include PsUtilities::PreBuiltGet
|
30
30
|
include PsUtilities::PreBuiltPut
|
@@ -33,6 +33,9 @@ module PsUtilities
|
|
33
33
|
def initialize(attributes: {}, headers: {})
|
34
34
|
@credentials = attr_defaults.merge(attributes)
|
35
35
|
@headers = header_defaults.merge(headers)
|
36
|
+
@base_uri = credentials[:base_uri]
|
37
|
+
@auth_path = credentials[:auth_endpoint]
|
38
|
+
@version = "v#{PsUtilities::Version::VERSION}"
|
36
39
|
|
37
40
|
raise ArgumentError, "missing client_secret" if credentials[:client_secret].nil? or
|
38
41
|
credentials[:client_secret].empty?
|
@@ -43,13 +46,13 @@ module PsUtilities
|
|
43
46
|
end
|
44
47
|
|
45
48
|
# with no command it just authenticates
|
46
|
-
def run(command: nil, params: {},
|
49
|
+
def run(command: nil, params: {}, api_path: "", options: {})
|
47
50
|
authenticate unless token_valid?
|
48
51
|
case command
|
49
52
|
when nil, :authenticate
|
50
53
|
# authenticate unless token_valid?
|
51
54
|
when :get, :put, :post
|
52
|
-
send(command,
|
55
|
+
send(command, api_path, options) unless api_path.empty?
|
53
56
|
else
|
54
57
|
send(command, params)
|
55
58
|
end
|
@@ -58,17 +61,17 @@ module PsUtilities
|
|
58
61
|
private
|
59
62
|
|
60
63
|
# options = {query: {}}
|
61
|
-
def get(
|
62
|
-
|
63
|
-
|
64
|
+
def get(api_path, options={})
|
65
|
+
count = 0
|
66
|
+
retries = 3
|
67
|
+
ps_url = base_uri + api_path
|
64
68
|
options = options.merge(headers)
|
65
|
-
ps_url = credentials[:base_uri] + url
|
66
69
|
begin
|
67
70
|
HTTParty.get(ps_url, options)
|
68
71
|
# self.class.get(url, query: options[:query], headers: options[:headers])
|
69
72
|
rescue Net::ReadTimeout, Net::OpenTimeout
|
70
|
-
if
|
71
|
-
|
73
|
+
if count < retries
|
74
|
+
count += 1
|
72
75
|
retry
|
73
76
|
else
|
74
77
|
{ error: "no response (timeout) from URL: #{url}" }
|
@@ -77,17 +80,17 @@ module PsUtilities
|
|
77
80
|
end
|
78
81
|
|
79
82
|
# options = {body: {}}
|
80
|
-
def put(
|
81
|
-
|
82
|
-
|
83
|
+
def put(api_path, options={})
|
84
|
+
count = 0
|
85
|
+
retries = 3
|
86
|
+
ps_url = base_uri + api_path
|
83
87
|
options = options.merge(headers)
|
84
|
-
ps_url = credentials[:base_uri] + url
|
85
88
|
begin
|
86
|
-
HTTParty.
|
87
|
-
# self.class.get(url,
|
89
|
+
HTTParty.get(ps_url, options)
|
90
|
+
# self.class.get(url, query: options[:query], headers: options[:headers])
|
88
91
|
rescue Net::ReadTimeout, Net::OpenTimeout
|
89
|
-
if
|
90
|
-
|
92
|
+
if count < retries
|
93
|
+
count += 1
|
91
94
|
retry
|
92
95
|
else
|
93
96
|
{ error: "no response (timeout) from URL: #{url}" }
|
@@ -96,17 +99,17 @@ module PsUtilities
|
|
96
99
|
end
|
97
100
|
|
98
101
|
# options = {body: {}}
|
99
|
-
def post(
|
100
|
-
|
101
|
-
|
102
|
+
def post(api_path, options={})
|
103
|
+
count = 0
|
104
|
+
retries = 3
|
105
|
+
ps_url = base_uri + api_path
|
102
106
|
options = options.merge(headers)
|
103
|
-
ps_url = credentials[:base_uri] + url
|
104
107
|
begin
|
105
|
-
HTTParty.
|
106
|
-
# self.class.get(url,
|
108
|
+
HTTParty.get(ps_url, options)
|
109
|
+
# self.class.get(url, query: options[:query], headers: options[:headers])
|
107
110
|
rescue Net::ReadTimeout, Net::OpenTimeout
|
108
|
-
if
|
109
|
-
|
111
|
+
if count < retries
|
112
|
+
count += 1
|
110
113
|
retry
|
111
114
|
else
|
112
115
|
{ error: "no response (timeout) from URL: #{url}" }
|
@@ -117,7 +120,8 @@ module PsUtilities
|
|
117
120
|
# In PowerSchool go to System>System Settings>Plugin Management Configuration>your plugin>Data Provider Configuration to manually check plugin expiration date
|
118
121
|
def authenticate
|
119
122
|
@headers[:headers] ||= {}
|
120
|
-
ps_url
|
123
|
+
ps_url = base_uri + auth_path
|
124
|
+
# ps_url = credentials[:base_uri] + credentials[:auth_endpoint]
|
121
125
|
response = HTTParty.post(ps_url, {headers: auth_headers,
|
122
126
|
body: 'grant_type=client_credentials'})
|
123
127
|
|
@@ -139,16 +143,16 @@ module PsUtilities
|
|
139
143
|
return true
|
140
144
|
end
|
141
145
|
|
142
|
-
def auth_headers
|
146
|
+
def auth_headers(creds64 = encode_credentials)
|
143
147
|
{ 'ContentType' => 'application/x-www-form-urlencoded;charset=UTF-8',
|
144
148
|
'Accept' => 'application/json',
|
145
|
-
'Authorization' => 'Basic ' +
|
149
|
+
'Authorization' => 'Basic ' + creds64
|
146
150
|
}
|
147
151
|
end
|
148
152
|
|
149
|
-
def encode_credentials
|
150
|
-
ps_auth_text = [
|
151
|
-
|
153
|
+
def encode_credentials(creds = credentials)
|
154
|
+
ps_auth_text = [ creds[:client_id],
|
155
|
+
creds[:client_secret]
|
152
156
|
].join(':')
|
153
157
|
Base64.encode64(ps_auth_text).gsub(/\n/, '')
|
154
158
|
end
|
data/lib/ps_utilities/version.rb
CHANGED