fragment-alpha-sdk 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fragment_client.rb +28 -15
- 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: 8bebeaf8a7e6c7a422cfcc85e6a060a270ed461c8119bfb76c69582d4362841d
|
4
|
+
data.tar.gz: 0b17b4bad7ccd4af2043ada105c53c6c1d1d3b2be8e7cfe467e416ee35fa78d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b3656f69339faec7845b37d3b42252a3a25b62f30a95154acf577a3fe2dfbf87ea5bd78c65140420956333be07687ec66a7b810b733efbf719cf3b5710f70e0
|
7
|
+
data.tar.gz: 91e9f7916930ad8c24d39e56b8c767474809ff9933bdcf3ba83fae7b03968c756805a9a4ecde9e2f4ef740b0110b14cdda31f92bc8c8e4237525691a0377a0a5
|
data/lib/fragment_client.rb
CHANGED
@@ -11,17 +11,17 @@ require 'sorbet-runtime'
|
|
11
11
|
# A support module for the client
|
12
12
|
module FragmentGraphQl
|
13
13
|
extend T::Sig
|
14
|
-
|
14
|
+
HTTP = T.let(GraphQL::Client.const_get(:HTTP).new('https://api.fragment.dev/graphql') do
|
15
15
|
extend T::Sig
|
16
16
|
sig { params(context: T.untyped).returns(T::Hash[T.untyped, T.untyped]) }
|
17
17
|
def headers(context)
|
18
18
|
{ 'Authorization' => 'Bearer %s' % context[:access_token] }
|
19
19
|
end
|
20
|
-
end
|
20
|
+
end, GraphQL::Client::HTTP)
|
21
21
|
|
22
22
|
Schema = T.let(GraphQL::Client.load_schema("#{__dir__}/fragment.schema.json"), T.untyped)
|
23
23
|
|
24
|
-
Client = T.let(GraphQL::Client.new(schema: Schema, execute:
|
24
|
+
Client = T.let(GraphQL::Client.new(schema: Schema, execute: HTTP), GraphQL::Client)
|
25
25
|
|
26
26
|
Queries = T.let(Client.parse(
|
27
27
|
File.read("#{__dir__}/queries.graphql")
|
@@ -32,9 +32,14 @@ end
|
|
32
32
|
class FragmentClient
|
33
33
|
extend T::Sig
|
34
34
|
|
35
|
-
sig
|
36
|
-
|
35
|
+
sig do
|
36
|
+
params(client_id: String, client_secret: String, extra_queries_filename: T.nilable(String),
|
37
|
+
execute: GraphQL::Client::HTTP).void
|
38
|
+
end
|
39
|
+
def initialize(client_id, client_secret, extra_queries_filename: nil, execute: FragmentGraphQl::HTTP)
|
40
|
+
@client = T.let(GraphQL::Client.new(schema: FragmentGraphQl::Schema, execute: execute), GraphQL::Client)
|
37
41
|
conn = create_conn(client_id, client_secret)
|
42
|
+
# TODO: the token may need to be refreshed if the client is around for a long time
|
38
43
|
@token = T.let(get_token(conn, client_id), String)
|
39
44
|
FragmentGraphQl::Queries.constants.each do |q|
|
40
45
|
name = q.to_s.gsub(/[a-z]([A-Z])/) do |m|
|
@@ -51,12 +56,16 @@ class FragmentClient
|
|
51
56
|
File.read(extra_queries_filename)
|
52
57
|
)
|
53
58
|
queries.constants.each do |q|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
59
|
+
define_method_from_query(q, queries)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def define_method_from_query(q, queries)
|
64
|
+
name = q.to_s.gsub(/[a-z]([A-Z])/) do |m|
|
65
|
+
format('%<lower>s_%<upper>s', lower: m[0], upper: m[1].downcase)
|
66
|
+
end.gsub(/^[A-Z]/, &:downcase)
|
67
|
+
define_singleton_method(name) do |v|
|
68
|
+
query(queries.const_get(q), v)
|
60
69
|
end
|
61
70
|
end
|
62
71
|
|
@@ -65,22 +74,26 @@ class FragmentClient
|
|
65
74
|
f.request :url_encoded
|
66
75
|
f.request :authorization, :basic, client_id, client_secret
|
67
76
|
f.adapter :net_http
|
77
|
+
f.response :raise_error
|
68
78
|
end, Faraday::Connection)
|
69
79
|
end
|
70
80
|
|
71
81
|
sig { params(query: T.untyped, variables: T.untyped).returns(T.untyped) }
|
72
82
|
def query(query, variables)
|
73
|
-
|
83
|
+
@client.query(query, variables: variables, context: { access_token: @token })
|
74
84
|
end
|
75
85
|
|
76
86
|
private
|
77
87
|
|
78
88
|
sig { params(conn: Faraday::Connection, client_id: String).returns(String) }
|
79
89
|
def get_token(conn, client_id)
|
80
|
-
|
81
|
-
|
90
|
+
begin
|
91
|
+
response = conn.post('/oauth2/token') do |req|
|
92
|
+
req.body = format('grant_type=client_credentials&scope=https://api.fragment.dev/*&client_id=%s', client_id)
|
93
|
+
end
|
94
|
+
rescue Faraday::ClientError => e
|
95
|
+
raise StandardError, "oauth Authentication failed: '%s'" % e.to_s
|
82
96
|
end
|
83
|
-
|
84
97
|
T.let(JSON.parse(response.body)['access_token'], String)
|
85
98
|
end
|
86
99
|
end
|