fragment-alpha-sdk 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/fragment_client.rb +37 -10
  3. data/lib/queries.graphql +271 -11
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: efc049f49b8a5204a8f2b4a73a71fb7cb414d7075edb84985028c669bf7d5ccf
4
- data.tar.gz: 61cb68b3406b030f63f5b5d2d8c23650a17506068e1475047d29a84de08896f9
3
+ metadata.gz: 8bebeaf8a7e6c7a422cfcc85e6a060a270ed461c8119bfb76c69582d4362841d
4
+ data.tar.gz: 0b17b4bad7ccd4af2043ada105c53c6c1d1d3b2be8e7cfe467e416ee35fa78d7
5
5
  SHA512:
6
- metadata.gz: 397c8208aaa6ad9ec8180d4f8df3a043dd7ec8a22e8391eabd95609da7ef63210ae24396697fc33877e4386426664511eea74abc975cba4103e678f00e6ce674
7
- data.tar.gz: 8493076e142f600db9703a44cbf3380d21c826051d072df6182c874904ad4e1b123327359ea9b9c545385f8a7525670ffddf15a19e5ff900cb6aed1f61a956f1
6
+ metadata.gz: 0b3656f69339faec7845b37d3b42252a3a25b62f30a95154acf577a3fe2dfbf87ea5bd78c65140420956333be07687ec66a7b810b733efbf719cf3b5710f70e0
7
+ data.tar.gz: 91e9f7916930ad8c24d39e56b8c767474809ff9933bdcf3ba83fae7b03968c756805a9a4ecde9e2f4ef740b0110b14cdda31f92bc8c8e4237525691a0377a0a5
@@ -11,20 +11,20 @@ require 'sorbet-runtime'
11
11
  # A support module for the client
12
12
  module FragmentGraphQl
13
13
  extend T::Sig
14
- http = GraphQL::Client.const_get(:HTTP).new('https://api.fragment.dev/graphql') do
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: http), GraphQL::Client)
24
+ Client = T.let(GraphQL::Client.new(schema: Schema, execute: HTTP), GraphQL::Client)
25
25
 
26
26
  Queries = T.let(Client.parse(
27
- File.open("#{__dir__}/queries.graphql").read
27
+ File.read("#{__dir__}/queries.graphql")
28
28
  ), T.untyped)
29
29
  end
30
30
 
@@ -32,9 +32,14 @@ end
32
32
  class FragmentClient
33
33
  extend T::Sig
34
34
 
35
- sig { params(client_id: String, client_secret: String).void }
36
- def initialize(client_id, client_secret)
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|
@@ -44,6 +49,24 @@ class FragmentClient
44
49
  query(FragmentGraphQl::Queries.const_get(q), v)
45
50
  end
46
51
  end
52
+
53
+ return if extra_queries_filename.nil?
54
+
55
+ queries = FragmentGraphQl::Client.parse(
56
+ File.read(extra_queries_filename)
57
+ )
58
+ queries.constants.each do |q|
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)
69
+ end
47
70
  end
48
71
 
49
72
  def create_conn(client_id, client_secret)
@@ -51,22 +74,26 @@ class FragmentClient
51
74
  f.request :url_encoded
52
75
  f.request :authorization, :basic, client_id, client_secret
53
76
  f.adapter :net_http
77
+ f.response :raise_error
54
78
  end, Faraday::Connection)
55
79
  end
56
80
 
57
81
  sig { params(query: T.untyped, variables: T.untyped).returns(T.untyped) }
58
82
  def query(query, variables)
59
- FragmentGraphQl::Client.query(query, variables: variables, context: { access_token: @token })
83
+ @client.query(query, variables: variables, context: { access_token: @token })
60
84
  end
61
85
 
62
86
  private
63
87
 
64
88
  sig { params(conn: Faraday::Connection, client_id: String).returns(String) }
65
89
  def get_token(conn, client_id)
66
- response = conn.post('/oauth2/token') do |req|
67
- req.body = format('grant_type=client_credentials&scope=https://api.fragment.dev/*&client_id=%s', client_id)
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
68
96
  end
69
-
70
97
  T.let(JSON.parse(response.body)['access_token'], String)
71
98
  end
72
99
  end
data/lib/queries.graphql CHANGED
@@ -1,5 +1,6 @@
1
1
  mutation CreateLedger($ik: ID!, $ledger: CreateLedgerInput!) {
2
2
  createLedger(ik: $ik, ledger: $ledger) {
3
+ __typename
3
4
  ... on CreateLedgerResult {
4
5
  ledger {
5
6
  id
@@ -36,6 +37,175 @@ mutation CreateLedgerAccounts(
36
37
  isIkReplay
37
38
  }
38
39
  }
40
+ ... on BadRequestError {
41
+ code
42
+ message
43
+ }
44
+ ... on InternalError {
45
+ code
46
+ message
47
+ }
48
+ }
49
+ }
50
+
51
+ mutation ReconcileTx($entry: LedgerEntryInput!) {
52
+ reconcileTx(entry: $entry) {
53
+ __typename
54
+ ... on ReconcileTxResult {
55
+ entry {
56
+ id
57
+ date
58
+ description
59
+ }
60
+ lines {
61
+ id
62
+ date
63
+ amount
64
+ type
65
+ }
66
+ }
67
+ ... on BadRequestError {
68
+ code
69
+ message
70
+ }
71
+ ... on InternalError {
72
+ code
73
+ message
74
+ }
75
+ }
76
+ }
77
+
78
+ mutation AddLedgerEntry($entry: LedgerEntryInput!, $ik: ID!) {
79
+ addLedgerEntry(entry: $entry, ik: $ik) {
80
+ __typename
81
+ ... on AddLedgerEntryResult {
82
+ entry {
83
+ id
84
+ date
85
+ description
86
+ }
87
+ lines {
88
+ id
89
+ date
90
+ description
91
+ amount
92
+ type
93
+ }
94
+ }
95
+ ... on BadRequestError {
96
+ code
97
+ message
98
+ }
99
+ ... on InternalError {
100
+ code
101
+ message
102
+ }
103
+ }
104
+ }
105
+
106
+ mutation UpdateLedger($ledger: LedgerMatchInput!, $update: UpdateLedgerInput!) {
107
+ updateLedger(ledger: $ledger, update: $update) {
108
+ __typename
109
+ ... on UpdateLedgerResult {
110
+ ledger {
111
+ id
112
+ name
113
+ }
114
+ }
115
+ ... on BadRequestError {
116
+ code
117
+ message
118
+ }
119
+ ... on InternalError {
120
+ code
121
+ message
122
+ }
123
+ }
124
+ }
125
+
126
+ mutation UpdateLedgerAccount(
127
+ $ledgerAccount: LedgerAccountMatchInput!
128
+ $update: UpdateLedgerAccountInput!
129
+ ) {
130
+ updateLedgerAccount(ledgerAccount: $ledgerAccount, update: $update) {
131
+ __typename
132
+ ... on UpdateLedgerAccountResult {
133
+ ledgerAccount {
134
+ id
135
+ name
136
+ }
137
+ }
138
+ ... on BadRequestError {
139
+ code
140
+ message
141
+ }
142
+ ... on InternalError {
143
+ code
144
+ message
145
+ }
146
+ }
147
+ }
148
+
149
+ mutation NewCustomLink($name: String!, $ik: ID!) {
150
+ createCustomLink(name: $name, ik: $ik) {
151
+ __typename
152
+ ... on CreateCustomLinkResult {
153
+ link {
154
+ id
155
+ name
156
+ }
157
+ isIkReplay
158
+ }
159
+ ... on BadRequestError {
160
+ code
161
+ message
162
+ }
163
+ ... on InternalError {
164
+ code
165
+ message
166
+ }
167
+ }
168
+ }
169
+
170
+ mutation SyncBankAccounts(
171
+ $link: LinkMatchInput!
172
+ $accounts: [CustomAccountInput!]!
173
+ ) {
174
+ syncCustomAccounts(link: $link, accounts: $accounts) {
175
+ __typename
176
+ ... on SyncCustomAccountsResult {
177
+ accounts {
178
+ id
179
+ externalId
180
+ name
181
+ currency {
182
+ code
183
+ customCurrencyId
184
+ }
185
+ }
186
+ }
187
+ ... on BadRequestError {
188
+ code
189
+ message
190
+ }
191
+ ... on InternalError {
192
+ code
193
+ message
194
+ }
195
+ }
196
+ }
197
+
198
+ mutation MakeBankTransfer($bankTransfer: MakeBankTransferInput!, $ik: ID!) {
199
+ makeBankTransfer(bankTransfer: $bankTransfer, ik: $ik) {
200
+ __typename
201
+ ... on MakeBankTransferResult {
202
+ bankTransfer {
203
+ id
204
+ description
205
+ status
206
+ }
207
+ isIkReplay
208
+ }
39
209
  ... on Error {
40
210
  code
41
211
  message
@@ -108,22 +278,112 @@ query Ledger($ledger: LedgerMatchInput!) {
108
278
  }
109
279
  }
110
280
 
111
- # copied from https://fragment.dev/api-reference#mutation-addLedgerEntry
112
- mutation AddLedgerEntry($ik: ID!, $entry: LedgerEntryInput!) {
113
- addLedgerEntry(ik: $ik, entry: $entry) {
114
- ... on AddLedgerEntryResult {
115
- entry {
281
+ query GetLedgerAccounts(
282
+ $ledgerId: ID!
283
+ $after: String
284
+ $first: Int
285
+ $before: String
286
+ ) {
287
+ ledger(ledger: { id: $ledgerId }) {
288
+ id
289
+ name
290
+ ledgerAccounts(after: $after, first: $first, before: $before) {
291
+ nodes {
116
292
  id
293
+ name
294
+ type
295
+ }
296
+ pageInfo {
297
+ hasNextPage
298
+ endCursor
299
+ hasPreviousPage
300
+ startCursor
301
+ }
302
+ }
303
+ }
304
+ }
305
+
306
+ query GetAccountLinesOnDate(
307
+ $ledgerAccountId: ID!
308
+ $filter: LedgerLinesFilterSet
309
+ ) {
310
+ ledgerAccount(ledgerAccount: { id: $ledgerAccountId }) {
311
+ lines(filter: $filter) {
312
+ nodes {
313
+ id
314
+ posted
117
315
  date
316
+ amount
317
+ description
318
+ ledgerEntryId
118
319
  }
119
320
  }
120
- ... on BadRequestError {
121
- message
122
- code
321
+ }
322
+ }
323
+
324
+ query GetLedgerEntries($ledgerAccountId: ID!, $filter: LedgerLinesFilterSet) {
325
+ ledgerAccount(ledgerAccount: { id: $ledgerAccountId }) {
326
+ lines(filter: $filter) {
327
+ nodes {
328
+ id
329
+ posted
330
+ date
331
+ amount
332
+ description
333
+ ledgerEntryId
334
+ }
123
335
  }
124
- ... on InternalError {
125
- message
126
- code
336
+ }
337
+ }
338
+
339
+ query GetLinkedLedgerAccounts(
340
+ $ledgerId: ID!
341
+ $filter: LedgerAccountsFilterSet!
342
+ ) {
343
+ ledger(ledger: { id: $ledgerId }) {
344
+ id
345
+ ledgerAccounts(filter: $filter) {
346
+ nodes {
347
+ id
348
+ name
349
+ linkedAccount {
350
+ id
351
+ name
352
+ }
353
+ }
354
+ }
355
+ }
356
+ }
357
+
358
+ query GetBalances($ledgerId: ID!) {
359
+ ledger(ledger: { id: $ledgerId }) {
360
+ ledgerAccounts {
361
+ nodes {
362
+ id
363
+ name
364
+ type
365
+
366
+ ownBalance
367
+ childBalance
368
+ balance
369
+
370
+ childBalances {
371
+ nodes {
372
+ currency {
373
+ code
374
+ }
375
+ amount
376
+ }
377
+ }
378
+ balances {
379
+ nodes {
380
+ currency {
381
+ code
382
+ }
383
+ amount
384
+ }
385
+ }
386
+ }
127
387
  }
128
388
  }
129
389
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fragment-alpha-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - fragment