Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
e243329e
"vscode:/vscode.git/clone" did not exist on "a971c598b59532671a271520227cfd2fd54b1cd0"
Commit
e243329e
authored
Jul 11, 2023
by
Michael Yang
Browse files
check api status
parent
2a66a116
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
11 deletions
+38
-11
api/client.go
api/client.go
+23
-5
cmd/cmd.go
cmd/cmd.go
+14
-5
server/routes.go
server/routes.go
+1
-1
No files found.
api/client.go
View file @
e243329e
...
@@ -10,6 +10,20 @@ import (
...
@@ -10,6 +10,20 @@ import (
"net/url"
"net/url"
)
)
type
StatusError
struct
{
StatusCode
int
Status
string
Message
string
}
func
(
e
StatusError
)
Error
()
string
{
if
e
.
Message
!=
""
{
return
fmt
.
Sprintf
(
"%s: %s"
,
e
.
Status
,
e
.
Message
)
}
return
e
.
Status
}
type
Client
struct
{
type
Client
struct
{
base
url
.
URL
base
url
.
URL
}
}
...
@@ -25,7 +39,7 @@ func NewClient(hosts ...string) *Client {
...
@@ -25,7 +39,7 @@ func NewClient(hosts ...string) *Client {
}
}
}
}
func
(
c
*
Client
)
stream
(
ctx
context
.
Context
,
method
,
path
string
,
data
any
,
callback
func
([]
byte
)
error
)
error
{
func
(
c
*
Client
)
stream
(
ctx
context
.
Context
,
method
,
path
string
,
data
any
,
fn
func
([]
byte
)
error
)
error
{
var
buf
*
bytes
.
Buffer
var
buf
*
bytes
.
Buffer
if
data
!=
nil
{
if
data
!=
nil
{
bts
,
err
:=
json
.
Marshal
(
data
)
bts
,
err
:=
json
.
Marshal
(
data
)
...
@@ -53,7 +67,7 @@ func (c *Client) stream(ctx context.Context, method, path string, data any, call
...
@@ -53,7 +67,7 @@ func (c *Client) stream(ctx context.Context, method, path string, data any, call
scanner
:=
bufio
.
NewScanner
(
response
.
Body
)
scanner
:=
bufio
.
NewScanner
(
response
.
Body
)
for
scanner
.
Scan
()
{
for
scanner
.
Scan
()
{
var
errorResponse
struct
{
var
errorResponse
struct
{
Error
string
`json:"error"`
Error
string
`json:"error
,omitempty
"`
}
}
bts
:=
scanner
.
Bytes
()
bts
:=
scanner
.
Bytes
()
...
@@ -61,11 +75,15 @@ func (c *Client) stream(ctx context.Context, method, path string, data any, call
...
@@ -61,11 +75,15 @@ func (c *Client) stream(ctx context.Context, method, path string, data any, call
return
fmt
.
Errorf
(
"unmarshal: %w"
,
err
)
return
fmt
.
Errorf
(
"unmarshal: %w"
,
err
)
}
}
if
len
(
errorResponse
.
Error
)
>
0
{
if
response
.
StatusCode
>=
400
{
return
fmt
.
Errorf
(
"stream: %s"
,
errorResponse
.
Error
)
return
StatusError
{
StatusCode
:
response
.
StatusCode
,
Status
:
response
.
Status
,
Message
:
errorResponse
.
Error
,
}
}
}
if
err
:=
callback
(
bts
);
err
!=
nil
{
if
err
:=
fn
(
bts
);
err
!=
nil
{
return
err
return
err
}
}
}
}
...
...
cmd/cmd.go
View file @
e243329e
...
@@ -7,6 +7,7 @@ import (
...
@@ -7,6 +7,7 @@ import (
"fmt"
"fmt"
"log"
"log"
"net"
"net"
"net/http"
"os"
"os"
"path"
"path"
"strings"
"strings"
...
@@ -34,8 +35,15 @@ func RunRun(cmd *cobra.Command, args []string) error {
...
@@ -34,8 +35,15 @@ func RunRun(cmd *cobra.Command, args []string) error {
switch
{
switch
{
case
errors
.
Is
(
err
,
os
.
ErrNotExist
)
:
case
errors
.
Is
(
err
,
os
.
ErrNotExist
)
:
if
err
:=
pull
(
args
[
0
]);
err
!=
nil
{
if
err
:=
pull
(
args
[
0
]);
err
!=
nil
{
var
apiStatusError
api
.
StatusError
if
!
errors
.
As
(
err
,
&
apiStatusError
)
{
return
err
return
err
}
}
if
apiStatusError
.
StatusCode
!=
http
.
StatusBadGateway
{
return
err
}
}
case
err
!=
nil
:
case
err
!=
nil
:
return
err
return
err
}
}
...
@@ -50,11 +58,12 @@ func pull(model string) error {
...
@@ -50,11 +58,12 @@ func pull(model string) error {
context
.
Background
(),
context
.
Background
(),
&
api
.
PullRequest
{
Model
:
model
},
&
api
.
PullRequest
{
Model
:
model
},
func
(
progress
api
.
PullProgress
)
error
{
func
(
progress
api
.
PullProgress
)
error
{
if
bar
==
nil
&&
progress
.
Percent
==
100
{
if
bar
==
nil
{
if
progress
.
Percent
==
100
{
// already downloaded
// already downloaded
return
nil
return
nil
}
}
if
bar
==
nil
{
bar
=
progressbar
.
DefaultBytes
(
progress
.
Total
)
bar
=
progressbar
.
DefaultBytes
(
progress
.
Total
)
}
}
...
...
server/routes.go
View file @
e243329e
...
@@ -108,7 +108,7 @@ func pull(c *gin.Context) {
...
@@ -108,7 +108,7 @@ func pull(c *gin.Context) {
remote
,
err
:=
getRemote
(
req
.
Model
)
remote
,
err
:=
getRemote
(
req
.
Model
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusBad
Request
,
gin
.
H
{
"error"
:
err
.
Error
()})
c
.
JSON
(
http
.
StatusBad
Gateway
,
gin
.
H
{
"error"
:
err
.
Error
()})
return
return
}
}
...
...
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