open-build-service-api 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +29 -2
- data/lib/api/about.rb +30 -0
- data/lib/api/projects.rb +31 -0
- data/lib/connection.rb +86 -0
- data/lib/exception.rb +28 -0
- data/lib/models/project.rb +15 -0
- data/lib/open-build-service-api.rb +12 -0
- data/lib/version.rb +1 -1
- metadata +28 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9bf527982bd90cbc256ebf065939281cf3f6448c3c77ae6de5bccf60e576a01b
|
4
|
+
data.tar.gz: 9de0695570ac504b8b7172cc251dcec5a1712495002c643743d286e1a35e0900
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23f3d09d60d92aa116caa2ad2296ed5ef9b6d525aff16e81e64a9c9905cb9f77eb1e91e359b6ac342ac7b1f45192b6abf84b4a7d0da66957f42ec5b30de46a5c
|
7
|
+
data.tar.gz: b9fe7c2dbec2cb50272097637f08236b505662dfc713b6158e0ddd794490716c9b40fa2cbf56bd9777bc077f6b9715c3be013b2ced4c5c9220d7032234508e18
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
## Version: 0.0.2 (alpha)
|
2
|
+
|
3
|
+
- Adds `Connection` module to establish a connection with a Build Service instance
|
4
|
+
- Adds `About` module to query information about the current Build Service instance
|
5
|
+
- Adds `Projects` module to allow searching and listing projects
|
6
|
+
- Requires a new dependency: `date` >= 2.0.0
|
7
|
+
|
8
|
+
## Version: 0.0.1 (alpha)
|
9
|
+
|
10
|
+
- Dummy release to register gem name on https://rubygems.org
|
data/README.md
CHANGED
@@ -1,2 +1,29 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# Open Build Service API - Library for Ruby
|
2
|
+
|
3
|
+
This library is a wrapper for the [Open Build Service API](https://github.com/openSUSE/open-build-service).
|
4
|
+
|
5
|
+
## How to Install
|
6
|
+
|
7
|
+
It is as simple as it can be:
|
8
|
+
```
|
9
|
+
gem install open-build-service-api
|
10
|
+
```
|
11
|
+
|
12
|
+
## How to Use
|
13
|
+
|
14
|
+
Please use the following links for references:
|
15
|
+
|
16
|
+
- [Wiki](https://github.com/mschnitzer/open-build-service-api/wiki)
|
17
|
+
- [Examples](https://github.com/mschnitzer/open-build-service-api/tree/master/examples)
|
18
|
+
|
19
|
+
## Contribution
|
20
|
+
|
21
|
+
Contribution to this library is highly appreciated. You can work on [issues](https://github.com/mschnitzer/open-build-service-api/issues) at any time and open a pull request. Just clone this repository, do your changes, and open a new pull request.
|
22
|
+
|
23
|
+
If you need help, please open a [new issue](https://github.com/mschnitzer/open-build-service-api/issues/new).
|
24
|
+
|
25
|
+
## Reporting Bugs
|
26
|
+
|
27
|
+
Please report bugs via [GitHub's issue tracker](https://github.com/mschnitzer/open-build-service-api/issues).
|
28
|
+
|
29
|
+
Security relevant issues should be reported via email: mschnitzer@suse.com
|
data/lib/api/about.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module OpenBuildServiceAPI
|
2
|
+
module API
|
3
|
+
class About
|
4
|
+
attr_reader :title, :description, :revision, :last_deployment, :commit
|
5
|
+
|
6
|
+
def initialize(connection)
|
7
|
+
@connection = connection
|
8
|
+
reload!
|
9
|
+
end
|
10
|
+
|
11
|
+
def reload!
|
12
|
+
response = @connection.send_request(:get, '/about')
|
13
|
+
data = Nokogiri::XML(response.body)
|
14
|
+
|
15
|
+
@title = data.xpath('//about/title').text
|
16
|
+
@description = data.xpath('//about/description').text
|
17
|
+
@revision = data.xpath('//about/revision').text
|
18
|
+
@commit = data.xpath('//about/commit').text
|
19
|
+
|
20
|
+
begin
|
21
|
+
@last_deployment = DateTime.parse(data.xpath('//about/last_deployment').text)
|
22
|
+
rescue ArgumentError
|
23
|
+
@last_deployment = nil
|
24
|
+
end
|
25
|
+
|
26
|
+
true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/api/projects.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module OpenBuildServiceAPI
|
2
|
+
module API
|
3
|
+
class Projects
|
4
|
+
def initialize(connection)
|
5
|
+
@connection = connection
|
6
|
+
end
|
7
|
+
|
8
|
+
def list
|
9
|
+
projects = Nokogiri::XML(@connection.send_request(:get, '/source').body)
|
10
|
+
projects.xpath('//entry').map {|project| Project.new(projects: self, name: project.attr('name')) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def find(name)
|
14
|
+
begin
|
15
|
+
project_data = Nokogiri::XML(@connection.send_request(:get, "/source/#{CGI.escape(name)}").body)
|
16
|
+
packages = project_data.xpath('//entry').map { |package| package.attr('name') }
|
17
|
+
|
18
|
+
Project.new(projects: self, name: name, packages: packages)
|
19
|
+
rescue NotFoundError
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def find!(name)
|
24
|
+
project = find(name)
|
25
|
+
|
26
|
+
raise ProjectNotFoundError.new("Project '#{name}' does not exist.") unless project
|
27
|
+
project
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/connection.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
module OpenBuildServiceAPI
|
2
|
+
class Connection
|
3
|
+
attr_reader :api_endpoint
|
4
|
+
|
5
|
+
def initialize(username, password, opts = {})
|
6
|
+
@username = username
|
7
|
+
@password = password
|
8
|
+
@api_endpoint = opts[:api_endpoint] ? opts[:api_endpoint] : 'https://api.opensuse.org'
|
9
|
+
@request_timeout = opts[:request_timeout] ? opts[:request_timeout].to_i : 10
|
10
|
+
@ca_file = opts[:ca_file]
|
11
|
+
|
12
|
+
# send a simple request to test authentication - it raises an exception if the credentials are wrong
|
13
|
+
send_request(:get, '/')
|
14
|
+
end
|
15
|
+
|
16
|
+
def send_request(method, path, params = {})
|
17
|
+
request_body = params[:request_body] if params[:request_body]
|
18
|
+
params.delete(:request_body)
|
19
|
+
|
20
|
+
path = "/#{path}" unless path.start_with?('/')
|
21
|
+
|
22
|
+
request_params = "?#{format_params(params)}" unless format_params(params).empty?
|
23
|
+
uri = URI("#{@api_endpoint}#{path}#{request_params}")
|
24
|
+
|
25
|
+
begin
|
26
|
+
request = Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == 'https'), open_timeout: @request_timeout, ca_file: @ca_file)
|
27
|
+
|
28
|
+
if method.to_s.downcase == 'post'
|
29
|
+
request_method = Net::HTTP::Post.new(uri)
|
30
|
+
elsif method.to_s.downcase == 'put'
|
31
|
+
request_method = Net::HTTP::Put.new(uri)
|
32
|
+
elsif method.to_s.downcase == 'get'
|
33
|
+
request_method = Net::HTTP::Get.new(uri)
|
34
|
+
end
|
35
|
+
|
36
|
+
request_method['Accept'] = 'application/xml'
|
37
|
+
request_method['User-Agent'] = "open-build-service-api (Ruby Gem Version: #{OpenBuildServiceAPI::VERSION})"
|
38
|
+
|
39
|
+
request_method.basic_auth(@username, @password)
|
40
|
+
request_method.body = request_body if request_body
|
41
|
+
|
42
|
+
response = request.request(request_method)
|
43
|
+
|
44
|
+
raise InternalServerError.new(response) if response.is_a?(Net::HTTPInternalServerError)
|
45
|
+
raise NotFoundError.new(response) if response.is_a?(Net::HTTPNotFound)
|
46
|
+
raise AuthenticationError.new(response, "Authentication failed. Please check your credentials.") if response.is_a?(Net::HTTPUnauthorized)
|
47
|
+
|
48
|
+
return response
|
49
|
+
rescue Errno::ECONNREFUSED, SocketError, Net::OpenTimeout => err
|
50
|
+
raise ConnectionError.new(err.to_s)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def about
|
55
|
+
return @ref_about if @ref_about
|
56
|
+
@ref_about = API::About.new(self)
|
57
|
+
end
|
58
|
+
|
59
|
+
def projects
|
60
|
+
return @ref_projects if @ref_projects
|
61
|
+
@ref_projects = API::Projects.new(self)
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def format_params(params)
|
67
|
+
values = params.values
|
68
|
+
formatted_params = ""
|
69
|
+
|
70
|
+
params.keys.each_with_index do |param, index|
|
71
|
+
formatted_params += "&" if index != 0
|
72
|
+
|
73
|
+
if values[index].is_a?(Array)
|
74
|
+
values[index].each do |array_value|
|
75
|
+
formatted_params += '&' if formatted_params[-1] != '&' && formatted_params.length > 0
|
76
|
+
formatted_params += "#{CGI.escape(param.to_s)}[]=#{CGI.escape(array_value.to_s)}"
|
77
|
+
end
|
78
|
+
else
|
79
|
+
formatted_params += "#{CGI.escape(param.to_s)}=#{CGI.escape(values[index].to_s)}"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
formatted_params
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/lib/exception.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module OpenBuildServiceAPI
|
2
|
+
# remote API network errors
|
3
|
+
class RemoteAPIError < Exception
|
4
|
+
attr_accessor :response, :message
|
5
|
+
|
6
|
+
def initialize(response, message=nil)
|
7
|
+
@response = response
|
8
|
+
@message = message ? message : response.body
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
@message
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class InternalServerError < RemoteAPIError; end
|
17
|
+
class AuthenticationError < RemoteAPIError; end
|
18
|
+
class NotFoundError < RemoteAPIError; end
|
19
|
+
|
20
|
+
# remote API errors
|
21
|
+
class APIError < Exception; end
|
22
|
+
class ProjectNotFoundError < APIError; end
|
23
|
+
|
24
|
+
# library specific exceptions
|
25
|
+
class GeneralError < Exception; end
|
26
|
+
class ConnectionError < GeneralError; end
|
27
|
+
class InvalidHTTPMethodWithBody < GeneralError; end
|
28
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module OpenBuildServiceAPI
|
2
|
+
class Project
|
3
|
+
attr_accessor :name, :projects, :packages
|
4
|
+
|
5
|
+
def initialize(params = {})
|
6
|
+
@name = params[:name]
|
7
|
+
@projects = params[:projects]
|
8
|
+
@packages = params[:packages]
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
@name
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,2 +1,14 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'date'
|
3
|
+
require 'net/http'
|
1
4
|
require 'nokogiri'
|
2
5
|
require_relative 'version'
|
6
|
+
require_relative 'exception'
|
7
|
+
require_relative 'connection'
|
8
|
+
|
9
|
+
# OBS API
|
10
|
+
require_relative 'api/about'
|
11
|
+
require_relative 'api/projects'
|
12
|
+
|
13
|
+
# Models
|
14
|
+
require_relative 'models/project'
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open-build-service-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manuel Schnitzer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: date
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.0.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.0.0
|
13
33
|
- !ruby/object:Gem::Dependency
|
14
34
|
name: nokogiri
|
15
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -36,7 +56,13 @@ executables: []
|
|
36
56
|
extensions: []
|
37
57
|
extra_rdoc_files: []
|
38
58
|
files:
|
59
|
+
- CHANGELOG.md
|
39
60
|
- README.md
|
61
|
+
- lib/api/about.rb
|
62
|
+
- lib/api/projects.rb
|
63
|
+
- lib/connection.rb
|
64
|
+
- lib/exception.rb
|
65
|
+
- lib/models/project.rb
|
40
66
|
- lib/open-build-service-api.rb
|
41
67
|
- lib/version.rb
|
42
68
|
homepage: https://github.com/mschnitzer/open-build-service-api
|