Class: Medidata::API::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/medidata/api/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host:, path_prefix: '', lang: 'de') ⇒ Client

  • Args :

    • host -> REST API client

    • lang -> Preferred language for error messages



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/medidata/api/client.rb', line 16

def initialize(host:, path_prefix: '', lang: 'de')
  @host = host
  @lang = lang
  rest = REST.new(
    host: host,
    url_path: [path_prefix],
    request_headers: {
      "Accept": "application/json",
      "Accept-Language": lang,
      "DenteoSecret": "yes_it's_us"
    },
  )

  @rest = rest
end

Instance Method Details

#auth(id, username, password) ⇒ Object

Set credentials for this Client’s instance

  • Args :

    • id -> Medidata client ID

    • username -> Username

    • password -> password



38
39
40
41
42
43
# File 'lib/medidata/api/client.rb', line 38

def auth(id, username, password)
  @rest.update_headers({
    "X-CLIENT-ID": id,
    "Authorization": "Basic " + Base64.encode64(username + ":" + password).strip
  })
end

#download(tref) ⇒ Object

Retrieve download’s payload

  • Args :

    • tref -> download’s transmission reference



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/medidata/api/client.rb', line 202

def download(tref)
  params = {
    "request_headers": {
      "Accept": "application/octet-stream"
    },
  }
  res = @rest.ela.downloads._(tref).get(params)
  case res.status_code
  when 200 then
    res.body
  when 400 then
    raise BadRequestError.new("bad request")
  when 401 then
    raise UnauthenticatedError.new("authentication required to download document")
  when 403 then
    raise PermissionError.new("wrong credentials to download document")
  when 404 then
    raise MissingError.new("download not found")
  else
    raise "Error downloading document <#{res.status_code}>"
  end
end

#downloads_confirm_receipt(tref) ⇒ Object

Confirm download receipt

All downloads received from Medidata are pending by default and clients have to manually mark them as “CONFIRMED”. That should be done on a regular basis according to Medidata.

  • Args :

    • tref -> download’s transmission reference



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/medidata/api/client.rb', line 175

def downloads_confirm_receipt(tref)
  params = {
    "request_body": {
      "status": "CONFIRMED"
    }
  }
  res = @rest.ela.downloads._(tref).status.put(params)
  case res.status_code
  when 200, 204 then
    true
  when 400 then
    raise BadRequestError.new("bad request")
  when 401 then
    raise UnauthenticatedError.new("authentication required to confirm notification receipt")
  when 403 then
    raise PermissionError.new("wrong credentials to confirm notification receipt")
  when 404 then
    raise MissingError.new("download not found")
  else
    raise "Error updating download status <#{res.status_code}>"
  end
end

#downloads_pending(limit: 100, offset: 0) ⇒ Object

Load pending downloads

  • Args :

    • limit -> The total number of downloads allowed

    • offset -> Pagination offset



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/medidata/api/client.rb', line 143

def downloads_pending(limit: 100, offset: 0)
  params = {
    "query_params": {
      "limit": limit,
      "offset": offset
    }
  }
  res = @rest.ela.downloads.get(params)
  case res.status_code
  when 200 then
    body = res.parsed_body
    raise "Invalid downloads response body" unless body.kind_of?(Array)
    body.map { |row| Medidata::API::Download.new(row) }
  when 400 then
    raise BadRequestError.new("bad request")
  when 401 then
    raise UnauthenticatedError.new("authentication required to load pending downloads")
  when 403 then
    raise PermissionError.new("wrong credentials to load pending downloads")
  else
    raise "Error fetching Medidata downloads <#{res.status_code}>"
  end
end

#notifications_confirm_receipt(id) ⇒ Object

Confirm notification receipt

All notifications received from Medidata are pending by default and clients have to manually mark them as “read”. That should be done on a regular basis according to Medidata.

  • Args :

    • id -> Notification unique identifier



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/medidata/api/client.rb', line 262

def notifications_confirm_receipt(id)
  params = {
    "request_body": {
      "notificationFetched": true
    }
  }
  res = @rest.ela.notifications._(id).status.put(params)
  case res.status_code
  when 200, 204 then
    true
  when 400 then
    raise BadRequestError.new("bad request")
  when 401 then
    raise UnauthenticatedError.new("authentication required to confirm notification receipt")
  when 403 then
    raise PermissionError.new("wrong credentials to confirm notification receipt")
  when 404 then
    raise MissingError.new("notification not found")
  else
    raise "Error updating notification status <#{res.status_code}>"
  end
end

#notifications_pending(limit: 100, offset: 0) ⇒ Object

Load pending notifications

  • Args :

    • limit -> The total number of notifications allowed

    • offset -> Pagination offset



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/medidata/api/client.rb', line 230

def notifications_pending(limit: 100, offset: 0)
  params = {
    "query_params": {
      "limit": limit,
      "offset": offset
    }
  }
  res = @rest.ela.notifications.get(params)
  case res.status_code
  when 200 then
    body = res.parsed_body
    raise "Invalid notifications response body" unless body.kind_of?(Array)
    body.map { |row| Medidata::API::Notification.new(row) }
  when 400 then
    raise BadRequestError.new("bad request")
  when 401 then
    raise UnauthenticatedError.new("authentication required to load pending notifications")
  when 403 then
    raise PermissionError.new("wrong credentials to load pending notifications")
  else
    raise "Error fetching Medidata notifications <#{res.status_code}>"
  end
end

#participants(limit: 100, offset: 0, query: nil) ⇒ Object

  • Args :

    • limit -> The total number of participants allowed

    • offset -> Pagination offset

    • query -> ParticipantsQuery object (optional)



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/medidata/api/client.rb', line 49

def participants(limit: 100, offset: 0, query: nil)
  qs = {
    "limit": limit,
    "offset": offset
  }
  if query
    qs["lawtype"] = query.lawType if query.lawType
    qs["name"] = query.name if query.name
    qs["glnparticipant"] = query.glnParticipant if query.glnParticipant
  end

  params = {
    "query_params": qs
  }
  res = @rest.ela.participants.get(params)
  case res.status_code
  when 200 then
    body = res.parsed_body
    raise "Invalid participants response body" unless body.kind_of?(Array)
    body.map { |row| Medidata::API::Participant.new(row) }
  when 400 then
    raise BadRequestError.new("bad request")
  when 401 then
    raise UnauthenticatedError.new("authentication required to load participants")
  when 403 then
    raise PermissionError.new("wrong credentials to load participants")
  else
    raise "Error fetching Medidata participants <#{res.status_code}>"
  end
end

#upload(file, info = nil) ⇒ Object

Upload a document to Medidata

  • Args :

    • file -> File to upload (e.g. Invoice XML)

    • info -> Medidata::API::UploadControlData (optional)



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/medidata/api/client.rb', line 85

def upload(file, info = nil)
  multipart = Medidata::API::MultipartPost.new
  multipart.with_binary key: "elauploadstream", value: file
  multipart.with_text key: "elauploadinfo", value: Medidata::API::UploadControlData.new(info).to_json if info

  bounday = "__MEDI_REST_IN_PEACE__"
  params = {
    "request_headers": {
      "Content-Type": "multipart/form-data; charset=utf-8; boundary=#{bounday}"
    },
    "request_body": multipart.build(bounday)
  }
  res = @rest.ela.uploads.post(params)
  case res.status_code
  when 201 then
    # Upload created
    Medidata::API::Upload.new(res.parsed_body)
  when 400 then
    raise BadRequestError.new("bad request")
  when 401 then
    raise UnauthenticatedError.new("authentication required to upload")
  when 403 then
    raise PermissionError.new("wrong credentials to upload")
  when 409 then
    # TODO: Extract transmission reference from response
    raise ConflictError.new("this file has already been uploaded")
  else
    raise "Error uploading to Medidata <#{res.status_code}>"
  end
end

#upload_status(tref) ⇒ Object

Check upload status

  • Args :

    • tref -> Upload’s transmission reference



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/medidata/api/client.rb', line 120

def upload_status(tref)
  res = @rest.ela.uploads._(tref).status.get
  case res.status_code
  when 200 then
    Medidata::API::Upload.new(res.parsed_body)
  when 400 then
    raise BadRequestError.new("bad request")
  when 401 then
    raise UnauthenticatedError.new("authentication required to check upload status")
  when 403 then
    raise PermissionError.new("wrong credentials to check upload status")
  when 404 then
    raise MissingError.new("upload status not found")
  else
    raise "Error fetching Medidata upload status <#{res.status_code}>"
  end
end