Class: Mxrb::OfficialMarketplace::ContentApi

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/official_marketplace/content_api.rb

Overview

Client and resolver for the documented Mendix Marketplace Content API.

Constant Summary collapse

BASE_URL =

rubocop:disable Metrics/ClassLength

'https://marketplace-api.mendix.com/v1'
AUTHORIZED_HOSTS =
%w[marketplace-api.mendix.com marketplace.mendix.com].freeze
CONTENT_LIMIT =
100
VERSION_LIMIT =
20
MENDIX_VERSION =
/\A(?:\d{1,3}|1000)(?:\.(?:\d{1,3}|1000)){0,2}\z/
MENDIX_BUILD_VERSION =
/\A(?:\d{1,3}|1000)(?:\.(?:\d{1,3}|1000)){2}\.\d{1,10}\z/
UUID =
/\A[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}\z/i

Instance Method Summary collapse

Constructor Details

#initialize(pat: nil, credentials: Credentials.new, client: HttpClient.new) ⇒ ContentApi

Returns a new instance of ContentApi.

Raises:



15
16
17
18
19
20
21
22
# File 'lib/mxrb/official_marketplace/content_api.rb', line 15

def initialize(pat: nil, credentials: Credentials.new, client: HttpClient.new)
  @pat = pat.to_s.strip
  @pat = ENV['MXRB_MENDIX_PAT'].to_s.strip if @pat.empty?
  @pat = credentials.pat.to_s.strip if @pat.empty?
  raise MarketplaceError, missing_pat_message if @pat.empty?

  @client = client
end

Instance Method Details

#all_versions(content_id, published_since: nil) ⇒ Object



79
80
81
82
83
# File 'lib/mxrb/official_marketplace/content_api.rb', line 79

def all_versions(content_id, published_since: nil)
  paginated(VERSION_LIMIT) do |limit, offset|
    versions(content_id, published_since:, limit:, offset:)
  end
end

#content(content_id) ⇒ Object



34
35
36
37
# File 'lib/mxrb/official_marketplace/content_api.rb', line 34

def content(content_id)
  identifier = positive_integer(content_id, 'content ID')
  request("/content/#{identifier}")
end

#download(version_id, destination, download_url: nil) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/mxrb/official_marketplace/content_api.rb', line 71

def download(version_id, destination, download_url: nil)
  identifier = uuid_filter(version_id)
  url = download_url || "#{BASE_URL}/versions/#{identifier}/download"
  uri = URI.parse(url)
  trusted = AUTHORIZED_HOSTS.include?(uri.host)
  @client.download(url, destination, authorization: trusted ? authorization : nil)
end

#find(identifier) ⇒ Object

Raises:



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mxrb/official_marketplace/content_api.rb', line 51

def find(identifier)
  return content(identifier) if identifier.to_s.match?(/\A\d+\z/)

  candidates(identifier).each do |name|
    matches = search(name:)
    return matches.first if matches.one?
    raise MarketplaceError, "Marketplace content name #{name.inspect} is ambiguous" \
      if matches.length > 1
  end
  raise MarketplaceError, "Marketplace content not found: #{identifier.inspect}"
end

#resolve(identifier, version: nil, mendix_version: nil, allow_vulnerable: false) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/mxrb/official_marketplace/content_api.rb', line 63

def resolve(identifier, version: nil, mendix_version: nil, allow_vulnerable: false)
  selected_content = find(identifier)
  selected_version = resolve_version(selected_content.fetch('contentId'), version, mendix_version)
  ensure_compatible!(selected_version, mendix_version) if mendix_version
  reject_vulnerable!(selected_version) unless allow_vulnerable
  package(selected_content, selected_version)
end

#search(name: nil, private: nil, approved: nil, published_since: nil, limit: 10, offset: 0) ⇒ Object

rubocop:disable Metrics/ParameterLists



24
25
26
27
28
29
30
31
32
# File 'lib/mxrb/official_marketplace/content_api.rb', line 24

def search(name: nil, private: nil, approved: nil, published_since: nil, # rubocop:disable Metrics/ParameterLists
           limit: 10, offset: 0)
  parameters = {
    name:, isPrivate: boolean_filter(private), isCompanyApproved: boolean_filter(approved),
    publishedSince: date_filter(published_since), limit: bounded_integer(limit, 1..CONTENT_LIMIT),
    offset: bounded_integer(offset, 0..)
  }.compact
  items(request('/content', parameters))
end

#versions(content_id, version_id: nil, mendix_version: nil, published_since: nil, limit: VERSION_LIMIT, offset: 0) ⇒ Object

rubocop:disable Metrics/ParameterLists



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mxrb/official_marketplace/content_api.rb', line 39

def versions(content_id, version_id: nil, mendix_version: nil, published_since: nil, # rubocop:disable Metrics/ParameterLists
             limit: VERSION_LIMIT, offset: 0)
  identifier = positive_integer(content_id, 'content ID')
  parameters = {
    versionId: uuid_filter(version_id),
    supportedMendixVersion: mendix_version_filter(mendix_version),
    publishedSince: date_filter(published_since),
    limit: bounded_integer(limit, 1..VERSION_LIMIT), offset: bounded_integer(offset, 0..)
  }.compact
  items(request("/content/#{identifier}/versions", parameters))
end