Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
4
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
B
bunqapi
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Test Cases
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Sybren A. Stüvel
bunqapi
Commits
f1f9b315
Commit
f1f9b315
authored
May 23, 2019
by
Sybren A. Stüvel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Struct merging works
parent
806a18b2
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
146 additions
and
34 deletions
+146
-34
.gitignore
.gitignore
+2
-2
device_server.go
device_server.go
+9
-6
installation.go
installation.go
+23
-24
merge_structs.go
merge_structs.go
+66
-0
mergetest/struct_merge.go
mergetest/struct_merge.go
+46
-0
query_account/query_account.go
query_account/query_account.go
+0
-2
No files found.
.gitignore
View file @
f1f9b315
/
bunq_credentials.yaml
/
bunq_key_private.pem
bunq_credentials.yaml
bunq_key_private.pem
device_server.go
View file @
f1f9b315
...
...
@@ -11,10 +11,11 @@ type deviceServerRequest struct {
PermittedIPs
[]
string
`json:"permitted_ips"`
}
type
wrappedDeviceServerResponse
struct
{
Response
[]
deviceServerResponse
`json:"Response"`
}
type
deviceServerResponse
struct
{
Response
[]
struct
{
ID
BunqID
`json:"Id`
}
`json:"Response"`
ID
BunqID
`json:"Id`
}
func
(
c
*
Client
)
CheckDeviceServer
(
description
string
,
permittedIPs
[]
string
)
{
...
...
@@ -32,13 +33,15 @@ func (c *Client) PostDeviceServer(description string, permittedIPs []string) {
PermittedIPs
:
permittedIPs
,
}
response
:=
d
eviceServerResponse
{}
errResp
:=
c
.
DoRequest
(
"POST"
,
"device-server"
,
&
payload
,
&
r
esponse
)
wrappedResponse
:=
wrappedD
eviceServerResponse
{}
errResp
:=
c
.
DoRequest
(
"POST"
,
"device-server"
,
&
payload
,
&
wrappedR
esponse
)
if
errResp
!=
nil
{
log
.
WithFields
(
errResp
.
LogFields
())
.
Fatal
(
"error performing device registration request"
)
}
response
:=
deviceServerResponse
{}
MergeStructs
(
wrappedResponse
.
Response
,
&
response
)
c
.
creds
.
DeviceID
=
response
.
Response
[
0
]
.
ID
.
ID
c
.
creds
.
DeviceID
=
response
.
ID
.
ID
logrus
.
WithField
(
"id"
,
c
.
creds
.
DeviceID
)
.
Info
(
"registered device device ID"
)
c
.
creds
.
Save
()
}
installation.go
View file @
f1f9b315
...
...
@@ -17,21 +17,23 @@ type installationRequest struct {
ClientPublicKey
string
`json:"client_public_key"`
}
type
wrappedInstallationResponse
struct
{
Response
[]
installationResponse
`json:"Response"`
}
type
installationResponse
struct
{
Response
[]
struct
{
ID
*
BunqID
`json:"Id"`
Token
*
struct
{
ID
int
`json:"id"`
Created
string
`json:"created"`
Updated
string
`json:"updated"`
Token
string
`json:"token"`
}
`json:"Token"`
ServerPublicKey
*
struct
{
Key
string
`json:"server_public_key"`
}
`json:"ServerPublicKey"`
}
`json:"Response"`
ID
*
BunqID
`json:"Id"`
Token
*
struct
{
ID
int
`json:"id"`
Created
string
`json:"created"`
Updated
string
`json:"updated"`
Token
string
`json:"token"`
}
`json:"Token"`
ServerPublicKey
*
struct
{
Key
string
`json:"server_public_key"`
}
`json:"ServerPublicKey"`
}
func
publicKeyString
(
privateKey
*
rsa
.
PrivateKey
)
string
{
...
...
@@ -56,20 +58,17 @@ func (c *Client) PostInstallation() {
ClientPublicKey
:
publicKeyString
(
c
.
privateKey
),
}
response
:=
i
nstallationResponse
{}
errResp
:=
c
.
DoRequest
(
"POST"
,
"installation"
,
&
payload
,
&
r
esponse
)
wrappedResponse
:=
wrappedI
nstallationResponse
{}
errResp
:=
c
.
DoRequest
(
"POST"
,
"installation"
,
&
payload
,
&
wrappedR
esponse
)
if
errResp
!=
nil
{
log
.
WithFields
(
errResp
.
LogFields
())
.
Fatal
(
"error performing installation request"
)
}
for
_
,
subResponse
:=
range
response
.
Response
{
if
subResponse
.
ServerPublicKey
!=
nil
{
c
.
creds
.
ServerPublicKey
=
subResponse
.
ServerPublicKey
.
Key
}
if
subResponse
.
Token
!=
nil
{
c
.
creds
.
InstallationToken
=
subResponse
.
Token
.
Token
}
}
response
:=
installationResponse
{}
MergeStructs
(
wrappedResponse
.
Response
,
&
response
)
c
.
creds
.
ServerPublicKey
=
response
.
ServerPublicKey
.
Key
c
.
creds
.
InstallationToken
=
response
.
Token
.
Token
log
.
WithFields
(
logrus
.
Fields
{
"installationToken"
:
c
.
creds
.
InstallationToken
,
...
...
merge_structs.go
0 → 100644
View file @
f1f9b315
package
bunqapi
import
(
"fmt"
"reflect"
)
// MergeStructs merges an array of structs into a single struct.
func
MergeStructs
(
responses
interface
{},
mergedResp
interface
{})
{
arrayType
:=
reflect
.
TypeOf
(
responses
)
if
arrayType
.
Kind
()
!=
reflect
.
Slice
&&
arrayType
.
Kind
()
!=
reflect
.
Array
{
panic
(
fmt
.
Sprintf
(
"responses should be a slice or array, not %v"
,
arrayType
.
Kind
()))
}
responsesValue
:=
reflect
.
ValueOf
(
responses
)
numResponses
:=
responsesValue
.
Len
()
respType
:=
arrayType
.
Elem
()
numFields
:=
respType
.
NumField
()
resultType
:=
reflect
.
TypeOf
(
mergedResp
)
if
resultType
.
Elem
()
!=
respType
{
panic
(
fmt
.
Sprintf
(
"mergedResp should be *%v, not %v"
,
respType
,
reflect
.
TypeOf
(
mergedResp
)))
}
resultValue
:=
reflect
.
ValueOf
(
mergedResp
)
.
Elem
()
for
index
:=
0
;
index
<
numResponses
;
index
++
{
// fmt.Printf("response #%d:\n", index)
responseValue
:=
responsesValue
.
Index
(
index
)
for
fieldnum
:=
0
;
fieldnum
<
numFields
;
fieldnum
++
{
// field := respType.Field(fieldnum)
fieldVal
:=
responseValue
.
Field
(
fieldnum
)
// Skip unexported fields.
if
!
fieldVal
.
CanSet
()
{
continue
}
// fmt.Printf(" - field %d: %10v ", fieldnum, field.Name)
useValue
:=
false
switch
fieldVal
.
Kind
()
{
case
reflect
.
Ptr
:
useValue
=
!
fieldVal
.
IsNil
()
// fmt.Printf("is pointer: %v", useValue)
case
reflect
.
String
:
useValue
=
fieldVal
.
String
()
!=
""
// fmt.Printf("is string : %v", useValue)
case
reflect
.
Bool
:
useValue
=
fieldVal
.
Bool
()
// fmt.Printf("is bool : %v", useValue)
default
:
// fmt.Printf("is kind : %v (not using)", fieldVal.Kind())
}
// fmt.Println()
if
!
useValue
{
continue
}
resultField
:=
resultValue
.
Field
(
fieldnum
)
resultField
.
Set
(
fieldVal
)
}
}
}
mergetest/struct_merge.go
0 → 100644
View file @
f1f9b315
package
main
import
(
"encoding/json"
"os"
"github.com/sirupsen/logrus"
"gitlab.com/dr.sybren/bunqapi"
)
type
BunqResponse
struct
{
Response
[]
SingleResponse
}
type
SingleResponse
struct
{
Field1
string
`json:"field1,omitempty"`
Field2
string
`json:"field2,omitempty"`
SomeOb
*
SubOb
`json:"someOb,omitempty"`
booltje
bool
`json:"booltje"`
}
type
SubOb
struct
{
SubField
string
`json:"subfield,omitempty"`
}
func
main
()
{
responses
:=
[]
SingleResponse
{
{
Field1
:
"field1"
},
{
SomeOb
:
&
SubOb
{
SubField
:
"subfield"
}},
{
booltje
:
true
},
}
fromBunq
:=
BunqResponse
{
Response
:
responses
,
}
merged
:=
SingleResponse
{}
bunqapi
.
MergeStructs
(
fromBunq
.
Response
,
&
merged
)
out
,
err
:=
json
.
MarshalIndent
(
merged
,
""
,
" "
)
if
err
!=
nil
{
logrus
.
WithError
(
err
)
.
Fatal
(
"unable to marshal result to JSON"
)
}
os
.
Stdout
.
Write
(
out
)
os
.
Stdout
.
WriteString
(
"
\n
"
)
}
query_account.go
→
query_account
/query_account
.go
View file @
f1f9b315
// +build ignore
package
main
import
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment