tangled
alpha
login
or
join now
mackuba.eu
/
didkit
1
fork
atom
A library for handling DID identifiers used in Bluesky AT Protocol
1
fork
atom
overview
issues
pulls
pipelines
added more request helpers
mackuba.eu
7 months ago
9ac59883
68b526d0
+36
1 changed file
expand all
collapse all
unified
split
lib
didkit
requests.rb
+36
lib/didkit/requests.rb
···
1
1
+
require 'json'
2
2
+
require 'net/http'
3
3
+
require 'uri'
4
4
+
1
5
module DIDKit
2
6
module Requests
3
7
def get_response(url, options = {})
···
34
38
else
35
39
return response
36
40
end
41
41
+
end
42
42
+
end
43
43
+
44
44
+
def get_data(url, options = {})
45
45
+
content_type = options.delete(:content_type)
46
46
+
response = get_response(url, options)
47
47
+
48
48
+
if response.is_a?(Net::HTTPSuccess) && content_type_matches(response, content_type) && (data = response.body)
49
49
+
data
50
50
+
else
51
51
+
raise APIError.new(response)
52
52
+
end
53
53
+
end
54
54
+
55
55
+
def get_json(url, options = {})
56
56
+
JSON.parse(get_data(url, options))
57
57
+
end
58
58
+
59
59
+
def content_type_matches(response, expected_type)
60
60
+
content_type = response['Content-Type']
61
61
+
62
62
+
case expected_type
63
63
+
when String
64
64
+
content_type == expected_type
65
65
+
when Regexp
66
66
+
content_type =~ expected_type
67
67
+
when :json
68
68
+
content_type =~ /^application\/json(;.*)?$/
69
69
+
when nil
70
70
+
true
71
71
+
else
72
72
+
raise ArgumentError, "Invalid expected_type: #{expected_type.inspect}"
37
73
end
38
74
end
39
75
end