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
32107803
Commit
32107803
authored
8 years ago
by
Christoph Glaubitz
Browse files
Options
Downloads
Patches
Plain Diff
Cleaned up documentation and NewJSONRequest
parent
fb7848d1
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
netbox/client.go
+13
-7
13 additions, 7 deletions
netbox/client.go
with
13 additions
and
7 deletions
netbox/client.go
+
13
−
7
View file @
32107803
...
@@ -87,7 +87,8 @@ func (c *Client) NewRequest(method string, endpoint string, options Valuer) (*ht
...
@@ -87,7 +87,8 @@ func (c *Client) NewRequest(method string, endpoint string, options Valuer) (*ht
//
//
// If a nil Valuer is specified, no query parameters will be sent with the
// If a nil Valuer is specified, no query parameters will be sent with the
// request.
// request.
// If a nil io.Reader, no body 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
)
{
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
// 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
// resides at a path like http://example.com/netbox/, API requests will
...
@@ -98,10 +99,12 @@ func (c *Client) NewDataRequest(method string, endpoint string, options Valuer,
...
@@ -98,10 +99,12 @@ func (c *Client) NewDataRequest(method string, endpoint string, options Valuer,
// Remove leading slash if there is one. This is necessary to be able to
// 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,
// concat url parts in a correct manner. We can not use path.Join here,
// because this always trims the trailing slash, which causes the
// because this always trims the trailing slash, which causes the
// Do function to always run into 301 and then retry
ing
the correct
// 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
// Location. With GET, it does work with one useless request, but it breaks
// each other http method.
// each other http method.
// Doing this, because out-of-tree extensions are more robust.
// 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
,
"/"
))
rel
,
err
:=
url
.
Parse
(
strings
.
TrimLeft
(
endpoint
,
"/"
))
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -125,22 +128,24 @@ func (c *Client) NewDataRequest(method string, endpoint string, options Valuer,
...
@@ -125,22 +128,24 @@ func (c *Client) NewDataRequest(method string, endpoint string, options Valuer,
// NewJSONRequest creates a HTTP request using the input HTTP method, URL
// NewJSONRequest creates a HTTP request using the input HTTP method, URL
// endpoint, a Valuer which creates URL parameters for the request, and
// endpoint, a Valuer which creates URL parameters for the request, and
// a io.Reader as the body of the request. For body, expecting some
// an io.Reader as the body of the request.
// json.Marshal-able struct. nil body is not allowed.
// NewJSONRequest also sets HTTP Header
// "Content-Type: application/json; utf-8"
//
//
// If a nil Valuer is specified, no query parameters will be sent with the
// If a nil Valuer is specified, no query parameters will be sent with the
// request.
// 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
)
{
func
(
c
*
Client
)
NewJSONRequest
(
method
string
,
endpoint
string
,
options
Valuer
,
body
interface
{})
(
*
http
.
Request
,
error
)
{
if
body
==
nil
{
if
body
==
nil
{
return
nil
,
errors
.
New
(
"expected body to be not nil"
)
return
nil
,
errors
.
New
(
"expected body to be not nil"
)
}
}
b
:=
new
(
bytes
.
Buffer
)
b
:=
new
(
bytes
.
Buffer
)
err
:=
json
.
NewEncoder
(
b
)
.
Encode
(
body
)
err
:=
json
.
NewEncoder
(
b
)
.
Encode
(
body
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
req
,
err
:=
c
.
NewDataRequest
(
req
,
err
:=
c
.
NewDataRequest
(
method
,
method
,
endpoint
,
endpoint
,
...
@@ -150,6 +155,7 @@ func (c *Client) NewJSONRequest(method string, endpoint string, options Valuer,
...
@@ -150,6 +155,7 @@ func (c *Client) NewJSONRequest(method string, endpoint string, options Valuer,
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json; charset=utf-8"
)
req
.
Header
.
Set
(
"Content-Type"
,
"application/json; charset=utf-8"
)
return
req
,
nil
return
req
,
nil
}
}
...
...
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