@@ -8,7 +8,7 @@ class ProfileApiClient
88
99 # rubocop:disable Naming/MethodName
1010 School = Data . define ( :id , :schoolCode , :updatedAt , :createdAt , :discardedAt )
11- SafeguardingFlag = Data . define ( :id , :userId , :flag , :email , :createdAt , :updatedAt , :discardedAt )
11+ SafeguardingFlag = Data . define ( :id , :userId , :schoolId , : flag, :email , :createdAt , :updatedAt , :discardedAt )
1212 Student = Data . define ( :id , :schoolId , :name , :username , :createdAt , :updatedAt , :discardedAt , :email , :ssoProviders )
1313 # rubocop:enable Naming/MethodName
1414
@@ -27,6 +27,8 @@ def initialize(errors)
2727 end
2828 end
2929
30+ class UnauthorizedError < Error ; end
31+
3032 class UnexpectedResponse < Error
3133 attr_reader :response_status , :response_headers , :response_body
3234
@@ -50,6 +52,7 @@ def create_school(token:, id:, code:)
5052 }
5153 end
5254
55+ unauthorized! ( response )
5356 raise UnexpectedResponse , response unless response . status == 201
5457
5558 School . new ( **response . body )
@@ -58,6 +61,7 @@ def create_school(token:, id:, code:)
5861 def school_student ( token :, school_id :, student_id :)
5962 response = connection ( token ) . get ( "/api/v1/schools/#{ school_id } /students/#{ student_id } " )
6063
64+ unauthorized! ( response )
6165 raise UnexpectedResponse , response unless response . status == 200
6266
6367 build_student ( response . body )
@@ -70,6 +74,7 @@ def list_school_students(token:, school_id:, student_ids:)
7074 request . body = student_ids
7175 end
7276
77+ unauthorized! ( response )
7378 raise UnexpectedResponse , response unless response . status == 200
7479
7580 response . body . map { |attrs | build_student ( attrs ) }
@@ -86,6 +91,7 @@ def create_school_student(token:, username:, password:, name:, school_id:)
8691 } ]
8792 end
8893
94+ unauthorized! ( response )
8995 raise UnexpectedResponse , response unless response . status == 201
9096
9197 response . body . deep_symbolize_keys
@@ -103,6 +109,7 @@ def validate_school_students(token:, students:, school_id:)
103109 request . headers [ 'Content-Type' ] = 'application/json'
104110 end
105111
112+ unauthorized! ( response )
106113 raise UnexpectedResponse , response unless response . status == 200
107114 rescue Faraday ::UnprocessableEntityError => e
108115 raise Student422Error , JSON . parse ( e . response_body ) [ 'errors' ]
@@ -119,6 +126,7 @@ def create_school_students(token:, students:, school_id:, preflight: false)
119126 request . headers [ 'Content-Type' ] = 'application/json'
120127 end
121128
129+ unauthorized! ( response )
122130 raise UnexpectedResponse , response unless [ 200 , 201 ] . include? ( response . status )
123131
124132 response . body . deep_symbolize_keys
@@ -136,6 +144,7 @@ def create_school_students_sso(token:, students:, school_id:)
136144 request . headers [ 'Content-Type' ] = 'application/json'
137145 end
138146
147+ unauthorized! ( response )
139148 raise UnexpectedResponse , response unless [ 200 , 201 ] . include? ( response . status )
140149
141150 response . body . map ( &:deep_symbolize_keys )
@@ -154,6 +163,7 @@ def update_school_student(token:, school_id:, student_id:, name: nil, username:
154163 } . compact
155164 end
156165
166+ unauthorized! ( response )
157167 raise UnexpectedResponse , response unless response . status == 200
158168
159169 build_student ( response . body )
@@ -166,28 +176,32 @@ def delete_school_student(token:, school_id:, student_id:)
166176
167177 response = connection ( token ) . delete ( "/api/v1/schools/#{ school_id } /students/#{ student_id } " )
168178
179+ unauthorized! ( response )
169180 raise UnexpectedResponse , response unless response . status == 204
170181 end
171182
172183 def safeguarding_flags ( token :)
173184 response = connection ( token ) . get ( '/api/v1/safeguarding-flags' )
174185
186+ unauthorized! ( response )
175187 raise UnexpectedResponse , response unless response . status == 200
176188
177189 response . body . map { |flag | SafeguardingFlag . new ( **flag . symbolize_keys ) }
178190 end
179191
180- def create_safeguarding_flag ( token :, flag :, email :)
192+ def create_safeguarding_flag ( token :, flag :, email :, school_id : )
181193 response = connection ( token ) . post ( '/api/v1/safeguarding-flags' ) do |request |
182- request . body = { flag :, email : }
194+ request . body = { flag :, email :, schoolId : school_id }
183195 end
184196
197+ unauthorized! ( response )
185198 raise UnexpectedResponse , response unless [ 201 , 303 ] . include? ( response . status )
186199 end
187200
188201 def delete_safeguarding_flag ( token :, flag :)
189202 response = connection ( token ) . delete ( "/api/v1/safeguarding-flags/#{ flag } " )
190203
204+ unauthorized! ( response )
191205 raise UnexpectedResponse , response unless response . status == 204
192206 end
193207
@@ -197,7 +211,7 @@ def connection(token)
197211 Faraday . new ( ENV . fetch ( 'IDENTITY_URL' ) ) do |faraday |
198212 faraday . request :json
199213 faraday . response :json
200- faraday . response :raise_error
214+ faraday . response :raise_error , allowed_statuses : [ 401 ]
201215 faraday . headers = {
202216 'Accept' => 'application/json' ,
203217 'Authorization' => "Bearer #{ token } " ,
@@ -229,5 +243,10 @@ def build_student(attrs)
229243
230244 Student . new ( **symbolized_attrs )
231245 end
246+
247+ def unauthorized! ( response )
248+ # The API is only available to verified non-student users that are over 13. Others get a 401.
249+ raise UnauthorizedError , 'Profile API unauthorized' if response . status == 401
250+ end
232251 end
233252end
0 commit comments