Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
go-netbox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IT Services
go-netbox
Commits
d7212af9
Commit
d7212af9
authored
8 years ago
by
Matt Layher
Committed by
GitHub
8 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #12 from chrigl/v2-prepare-POST
Prepared client for POST/PUT/PATCH/DELETE
parents
93500c8d
32107803
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
netbox/client.go
+71
-9
71 additions, 9 deletions
netbox/client.go
netbox/client_test.go
+41
-0
41 additions, 0 deletions
netbox/client_test.go
with
112 additions
and
9 deletions
netbox/client.go
+
71
−
9
View file @
d7212af9
...
...
@@ -15,12 +15,15 @@
package
netbox
import
(
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"
path
"
"
strings
"
)
// A Client is a NetBox client. It can be used to retrieve network and
...
...
@@ -46,6 +49,13 @@ func NewClient(addr string, client *http.Client) (*Client, error) {
client
=
&
http
.
Client
{}
}
// Append trailing slash there is none. This is necessary
// to be able to concat url parts in a correct manner.
// See NewRequest
if
!
strings
.
HasSuffix
(
addr
,
"/"
)
{
addr
=
addr
+
"/"
}
u
,
err
:=
url
.
Parse
(
addr
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -68,25 +78,43 @@ func NewClient(addr string, client *http.Client) (*Client, error) {
// If a nil Valuer is specified, no query parameters will be sent with the
// request.
func
(
c
*
Client
)
NewRequest
(
method
string
,
endpoint
string
,
options
Valuer
)
(
*
http
.
Request
,
error
)
{
rel
,
err
:=
url
.
Parse
(
endpoint
)
if
err
!=
nil
{
return
nil
,
err
}
return
c
.
NewDataRequest
(
method
,
endpoint
,
options
,
nil
)
}
// NewDataRequest creates a HTTP request using the input HTTP method, URL
// endpoint, a Valuer which creates URL parameters for the request, and
// a io.Reader as the body of the request.
//
// If a nil Valuer is specified, no query parameters will be sent with the
// request.
//
// If a nil io.Reader is specified, no body will be sent with the request.
func
(
c
*
Client
)
NewDataRequest
(
method
string
,
endpoint
string
,
options
Valuer
,
body
io
.
Reader
)
(
*
http
.
Request
,
error
)
{
// Allow specifying a base path for API requests, so if a NetBox server
// resides at a path like http://example.com/netbox/, API requests will
// be sent to http://example.com/netbox/api/...
//
// Enables support of: https://github.com/digitalocean/netbox/issues/212.
if
c
.
u
.
Path
!=
""
{
rel
.
Path
=
path
.
Join
(
c
.
u
.
Path
,
rel
.
Path
)
//
// Remove leading slash if there is one. This is necessary to be able to
// concat url parts in a correct manner. We can not use path.Join here,
// because this always trims the trailing slash, which causes the
// Do function to always run into 301 and then retry the correct
// Location. With GET, it does work with one useless request, but it breaks
// each other http method.
// Doing this, because out-of-tree extensions are more robust. If someone
// implements an own API-call, we do not override parts of c.u, even if
// the caller uses "/api/...".
rel
,
err
:=
url
.
Parse
(
strings
.
TrimLeft
(
endpoint
,
"/"
))
if
err
!=
nil
{
return
nil
,
err
}
u
:=
c
.
u
.
ResolveReference
(
rel
)
// If no valuer specified, create a request with no query parameters
if
options
==
nil
{
return
http
.
NewRequest
(
method
,
u
.
String
(),
nil
)
return
http
.
NewRequest
(
method
,
u
.
String
(),
body
)
}
values
,
err
:=
options
.
Values
()
...
...
@@ -95,7 +123,41 @@ func (c *Client) NewRequest(method string, endpoint string, options Valuer) (*ht
}
u
.
RawQuery
=
values
.
Encode
()
return
http
.
NewRequest
(
method
,
u
.
String
(),
nil
)
return
http
.
NewRequest
(
method
,
u
.
String
(),
body
)
}
// NewJSONRequest creates a HTTP request using the input HTTP method, URL
// endpoint, a Valuer which creates URL parameters for the request, and
// an io.Reader as the body of the request.
//
// If a nil Valuer is specified, no query parameters will be sent with the
// request.
//
// The body parameter is marshaled to JSON and sent as a HTTP request body.
// Body must not be nil.
func
(
c
*
Client
)
NewJSONRequest
(
method
string
,
endpoint
string
,
options
Valuer
,
body
interface
{})
(
*
http
.
Request
,
error
)
{
if
body
==
nil
{
return
nil
,
errors
.
New
(
"expected body to be not nil"
)
}
b
:=
new
(
bytes
.
Buffer
)
err
:=
json
.
NewEncoder
(
b
)
.
Encode
(
body
)
if
err
!=
nil
{
return
nil
,
err
}
req
,
err
:=
c
.
NewDataRequest
(
method
,
endpoint
,
options
,
b
,
)
if
err
!=
nil
{
return
nil
,
err
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json; charset=utf-8"
)
return
req
,
nil
}
// Do executes an HTTP request and if v is not nil, Do unmarshals result
...
...
This diff is collapsed.
Click to expand it.
netbox/client_test.go
+
41
−
0
View file @
d7212af9
...
...
@@ -18,6 +18,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
...
...
@@ -95,6 +96,46 @@ func TestClientBadStatusCode(t *testing.T) {
}
}
func
TestNewJSONRequest
(
t
*
testing
.
T
)
{
c
,
done
:=
testClient
(
t
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
Write
([]
byte
(
"foo"
))
})
defer
done
()
wantBody
:=
"{
\"
id
\"
:1,
\"
name
\"
:
\"
Test 1
\"
}
\n
"
wantHeader
:=
"application/json; charset=utf-8"
req
,
err
:=
c
.
NewJSONRequest
(
http
.
MethodPost
,
"/"
,
nil
,
&
struct
{
ID
int
`json:"id"`
Name
string
`json:"name"`
}{
ID
:
1
,
Name
:
"Test 1"
,
})
if
err
!=
nil
{
t
.
Fatal
(
"expected no error, but an error returned"
)
}
res
,
err
:=
ioutil
.
ReadAll
(
req
.
Body
)
if
err
!=
nil
{
t
.
Fatal
(
"expected no error, but an error returned"
)
}
if
want
,
got
:=
wantBody
,
string
(
res
);
got
!=
want
{
t
.
Fatalf
(
"unexpected body:
\n
- want: %v
\n
- got: %v"
,
want
,
got
)
}
if
want
,
got
:=
wantHeader
,
req
.
Header
.
Get
(
"Content-Type"
);
got
!=
want
{
t
.
Fatalf
(
"unexpected body:
\n
- want: %v
\n
- got: %v"
,
want
,
got
)
}
req
,
err
=
c
.
NewJSONRequest
(
http
.
MethodPost
,
"/"
,
nil
,
nil
)
if
err
==
nil
{
t
.
Fatal
(
"expected an error, but there was none"
)
}
if
req
!=
nil
{
t
.
Fatalf
(
"expected a nil request, but got %v"
,
req
)
}
}
func
TestClientQueryParameters
(
t
*
testing
.
T
)
{
c
:=
&
Client
{
u
:
&
url
.
URL
{},
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment