fragment-alpha-sdk 0.1.9 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/fragment_client.rb +46 -32
- data/lib/queries.graphql +2 -2
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f561429ab3fdfcc3fbebaf0fdf6a19c175d086b9de97ebd4f21c882456d7b027
|
4
|
+
data.tar.gz: 48505cd86e60a4a65fdad012849991c43ddf4aa88ff8d96916aed1d96302040f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3e1d5191c62daecf13f69614da1700dfb9e678bd893efb9e131a08883769bd9efd60cd8bd907e98f0c3309975ca2709f46b204c28dcfc487a04c781dd7eeaf4
|
7
|
+
data.tar.gz: 934673065a76e5e9d4cdcf49bf8e01be2dd60ed3fedeb6a2b8bab84bff82f0bcec6989f91a7343aa613da9eda554f05b7acca5f7f40937bbbe6e818f7b815df9
|
data/lib/fragment_client.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# typed:
|
1
|
+
# typed: false
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'faraday'
|
@@ -7,25 +7,29 @@ require 'json'
|
|
7
7
|
require 'graphql/client'
|
8
8
|
require 'graphql/client/http'
|
9
9
|
require 'sorbet-runtime'
|
10
|
+
require 'uri'
|
10
11
|
|
11
12
|
# A support module for the client
|
12
13
|
module FragmentGraphQl
|
13
14
|
extend T::Sig
|
14
|
-
|
15
|
+
|
16
|
+
CustomHTTP = Class.new(GraphQL::Client::HTTP) do
|
15
17
|
extend T::Sig
|
16
18
|
sig { params(context: T.untyped).returns(T::Hash[T.untyped, T.untyped]) }
|
17
19
|
def headers(context)
|
18
20
|
{ 'Authorization' => format('Bearer %s', context[:access_token]) }
|
19
21
|
end
|
20
|
-
end
|
22
|
+
end
|
23
|
+
|
24
|
+
HTTP = T.let(CustomHTTP.new('https://api.fragment.dev/graphql'), GraphQL::Client::HTTP)
|
21
25
|
|
22
|
-
|
26
|
+
FragmentSchema = T.let(GraphQL::Client.load_schema("#{__dir__}/fragment.schema.json"), T.untyped)
|
23
27
|
|
24
|
-
Client = T.let(GraphQL::Client.new(schema:
|
28
|
+
Client = T.let(GraphQL::Client.new(schema: FragmentSchema, execute: HTTP), GraphQL::Client)
|
25
29
|
|
26
|
-
|
27
|
-
|
28
|
-
|
30
|
+
FragmentQueries = T.let(Client.parse(
|
31
|
+
File.read("#{__dir__}/queries.graphql")
|
32
|
+
), T.untyped)
|
29
33
|
end
|
30
34
|
|
31
35
|
# A client for Fragment
|
@@ -34,26 +38,41 @@ class FragmentClient
|
|
34
38
|
|
35
39
|
sig do
|
36
40
|
params(client_id: String, client_secret: String, extra_queries_filename: T.nilable(String),
|
37
|
-
execute: GraphQL::Client::HTTP).
|
41
|
+
execute: T.nilable(GraphQL::Client::HTTP), api_url: T.nilable(String),
|
42
|
+
oauth_url: T.nilable(String), oauth_scope: T.nilable(String)).void
|
38
43
|
end
|
39
|
-
def initialize(client_id, client_secret, extra_queries_filename: nil, execute:
|
40
|
-
|
41
|
-
|
44
|
+
def initialize(client_id, client_secret, extra_queries_filename: nil, execute: nil, api_url: nil,
|
45
|
+
oauth_url: 'https://auth.fragment.dev/oauth2/token', oauth_scope: 'https://api.fragment.dev/*')
|
46
|
+
@oauth_scope = T.let(oauth_scope, String)
|
47
|
+
@oauth_url = T.let(URI.parse(oauth_url), URI)
|
48
|
+
@client_id = T.let(client_id, String)
|
49
|
+
@client_secret = T.let(client_secret, String)
|
50
|
+
|
51
|
+
execute ||= api_url ? FragmentGraphQl::CustomHTTP.new(URI.parse(api_url)) : FragmentGraphQl::HTTP
|
52
|
+
@execute = T.let(execute, GraphQL::Client::HTTP)
|
53
|
+
|
54
|
+
@client = T.let(GraphQL::Client.new(schema: FragmentGraphQl::FragmentSchema, execute: @execute), GraphQL::Client)
|
55
|
+
@conn = T.let(create_conn, Faraday::Connection)
|
56
|
+
# TODO: the token may need to be refreshed if the client is around for a long time
|
57
|
+
@token = T.let(create_token, String)
|
58
|
+
|
59
|
+
define_method_from_queries(FragmentGraphQl::FragmentQueries)
|
42
60
|
return if extra_queries_filename.nil?
|
43
61
|
|
44
|
-
queries =
|
62
|
+
queries = @client.parse(
|
45
63
|
File.read(extra_queries_filename)
|
46
64
|
)
|
47
65
|
define_method_from_queries(queries)
|
48
66
|
end
|
49
67
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
@token = T.let(get_token(conn, client_id), String)
|
68
|
+
|
69
|
+
sig { params(query: T.untyped, variables: T.untyped).returns(T.untyped) }
|
70
|
+
def query(query, variables)
|
71
|
+
@client.query(query, variables: variables, context: { access_token: @token })
|
55
72
|
end
|
56
73
|
|
74
|
+
private
|
75
|
+
|
57
76
|
def define_method_from_queries(queries)
|
58
77
|
queries.constants.each do |qry|
|
59
78
|
name = qry.to_s.gsub(/[a-z]([A-Z])/) do |m|
|
@@ -65,27 +84,22 @@ class FragmentClient
|
|
65
84
|
end
|
66
85
|
end
|
67
86
|
|
68
|
-
|
69
|
-
|
87
|
+
sig { returns(Faraday::Connection) }
|
88
|
+
def create_conn
|
89
|
+
T.let(Faraday.new(@oauth_url) do |f|
|
70
90
|
f.request :url_encoded
|
71
|
-
f.request :authorization, :basic, client_id, client_secret
|
91
|
+
f.request :authorization, :basic, @client_id, @client_secret
|
72
92
|
f.adapter :net_http
|
73
93
|
f.response :raise_error
|
74
94
|
end, Faraday::Connection)
|
75
95
|
end
|
76
96
|
|
77
|
-
sig {
|
78
|
-
def
|
79
|
-
@client.query(query, variables: variables, context: { access_token: @token })
|
80
|
-
end
|
81
|
-
|
82
|
-
private
|
83
|
-
|
84
|
-
sig { params(conn: Faraday::Connection, client_id: String).returns(String) }
|
85
|
-
def get_token(conn, client_id)
|
97
|
+
sig { returns(String) }
|
98
|
+
def create_token
|
86
99
|
begin
|
87
|
-
response = conn.post
|
88
|
-
req.body = format('grant_type=client_credentials&scope
|
100
|
+
response = @conn.post do |req|
|
101
|
+
req.body = format('grant_type=client_credentials&scope=%<scope>s&client_id=%<id>s', scope: @oauth_scope,
|
102
|
+
id: @client_id)
|
89
103
|
end
|
90
104
|
rescue Faraday::ClientError => e
|
91
105
|
raise StandardError, format("oauth Authentication failed: '%s'", e.to_s)
|
data/lib/queries.graphql
CHANGED
@@ -21,8 +21,8 @@ mutation StoreSchema($schema: SchemaInput!) {
|
|
21
21
|
}
|
22
22
|
}
|
23
23
|
|
24
|
-
mutation CreateLedger($ik: SafeString!, $ledger: CreateLedgerInput!) {
|
25
|
-
createLedger(ik: $ik, ledger: $ledger) {
|
24
|
+
mutation CreateLedger($ik: SafeString!, $ledger: CreateLedgerInput!, $schema: SchemaMatchInput!) {
|
25
|
+
createLedger(ik: $ik, ledger: $ledger, schema: $schema) {
|
26
26
|
__typename
|
27
27
|
... on CreateLedgerResult {
|
28
28
|
ledger {
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fragment-alpha-sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fragment
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 1980-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0.5'
|
55
|
-
description:
|
55
|
+
description:
|
56
56
|
email: snoble@fragment.dev
|
57
57
|
executables: []
|
58
58
|
extensions: []
|
@@ -66,7 +66,7 @@ homepage: https://fragment.dev
|
|
66
66
|
licenses:
|
67
67
|
- Nonstandard
|
68
68
|
metadata: {}
|
69
|
-
post_install_message:
|
69
|
+
post_install_message:
|
70
70
|
rdoc_options: []
|
71
71
|
require_paths:
|
72
72
|
- lib
|
@@ -81,8 +81,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
requirements: []
|
84
|
-
rubygems_version: 3.
|
85
|
-
signing_key:
|
84
|
+
rubygems_version: 3.5.4
|
85
|
+
signing_key:
|
86
86
|
specification_version: 4
|
87
87
|
summary: an alpha version for the fragment client sdk
|
88
88
|
test_files: []
|