Class: Gitlab::Lfs::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/lfs/client.rb

Overview

Gitlab::Lfs::Client implements a simple LFS client, designed to talk to LFS servers as described in these documents:

* https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md
* https://github.com/git-lfs/git-lfs/blob/master/docs/api/basic-transfers.md

Defined Under Namespace

Classes: BatchSubmitError, HttpError, ObjectUploadError, ObjectVerifyError, UnsupportedTransferError

Constant Summary collapse

GIT_LFS_CONTENT_TYPE =
'application/vnd.git-lfs+json'
GIT_LFS_USER_AGENT =
"GitLab #{Gitlab::VERSION} LFS client"
DEFAULT_HEADERS =
{
  'Accept' => GIT_LFS_CONTENT_TYPE,
  'Content-Type' => GIT_LFS_CONTENT_TYPE,
  'User-Agent' => GIT_LFS_USER_AGENT
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, credentials:) ⇒ Client

Returns a new instance of Client.


19
20
21
22
# File 'lib/gitlab/lfs/client.rb', line 19

def initialize(base_url, credentials:)
  @base_url = base_url
  @credentials = credentials
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.


17
18
19
# File 'lib/gitlab/lfs/client.rb', line 17

def base_url
  @base_url
end

Instance Method Details

#batch!(operation, objects) ⇒ Object

Raises:


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gitlab/lfs/client.rb', line 24

def batch!(operation, objects)
  body = {
    operation: operation,
    transfers: ['basic'],
    # We don't know `ref`, so can't send it
    objects: objects.as_json(only: [:oid, :size])
  }

  rsp = Gitlab::HTTP.post(
    batch_url,
    basic_auth: basic_auth,
    body: body.to_json,
    headers: build_request_headers
  )

  raise BatchSubmitError.new(http_response: rsp) unless rsp.success?

  # HTTParty provides rsp.parsed_response, but it only kicks in for the
  # application/json content type in the response, which we can't rely on
  body = Gitlab::Json.parse(rsp.body)
  transfer = body.fetch('transfer', 'basic')

  raise UnsupportedTransferError, transfer.inspect unless transfer == 'basic'

  body
end

#upload!(object, upload_action, authenticated:) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gitlab/lfs/client.rb', line 51

def upload!(object, upload_action, authenticated:)
  file = object.file.open

  params = {
    body_stream: file,
    headers: upload_headers(object, upload_action)
  }

  url = set_basic_auth_and_extract_lfs_url!(params, upload_action['href'])
  rsp = Gitlab::HTTP.put(url, params)

  raise ObjectUploadError.new(http_response: rsp) unless rsp.success?
ensure
  file&.close
end

#verify!(object, verify_action, authenticated:) ⇒ Object

Raises:


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gitlab/lfs/client.rb', line 67

def verify!(object, verify_action, authenticated:)
  params = {
    body: object.to_json(only: [:oid, :size]),
    headers: build_request_headers(verify_action['header'])
  }

  url = set_basic_auth_and_extract_lfs_url!(params, verify_action['href'])
  rsp = Gitlab::HTTP.post(url, params)

  raise ObjectVerifyError.new(http_response: rsp) unless rsp.success?
end