Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename language to service #29

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions Sources/BbcNews/BbcNews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,24 @@ public struct BbcNews {
/// Converts a BBC News webpage URL to a native API URL that returns a JSON representation, if one exists.
///
/// - Parameters:
/// - urlString: The language that results should be localised for.
/// - language: The language that results should be localised for.
/// - urlString: The URL to convert.
/// - service: The international service that API results should be localised for e.g. BBC Cymru.
/// - release: The release track of the API version.
/// - Returns: The native API URL, if successful.
public static func convertWebUrlToApi(urlString: String, language: Language = .english, release: ReleaseTrack? = nil) -> String? {
public static func convertWebUrlToApi(urlString: String, service: Service = .english, release: ReleaseTrack? = nil) -> String? {
guard let url = URL(string: urlString) else { return nil }
let result = BbcNews.convertWebUrlToApi(url: url, language: language, release: release)
let result = BbcNews.convertWebUrlToApi(url: url, service: service, release: release)
return result?.absoluteString
}

/// Converts a BBC News webpage URL to a native API URL that returns a JSON representation, if one exists.
///
/// - Parameters:
/// - urlString: The language that results should be localised for.
/// - language: The language that results should be localised for.
/// - url: The URL to convert.
/// - service: The international service that API results should be localised for e.g. BBC Cymru.
/// - release: The release track of the API version.
/// - Returns: The native API URL, if successful.
public static func convertWebUrlToApi(url: URL, language: Language = .english, release: ReleaseTrack? = nil) -> URL? {
public static func convertWebUrlToApi(url: URL, service: Service = .english, release: ReleaseTrack? = nil) -> URL? {
let regex = #/https?:\/\/(www\.)?bbc\.co(m|\.uk)\/news\/(\w|\-|\/)+(\.app)?$/#

// swiftlint:disable:next unused_optional_binding
Expand All @@ -80,7 +80,7 @@ public struct BbcNews {
components.host = "news-app.api.bbc.co.uk"
components.path = "/fd/app-article-api"
components.queryItems = [
URLQueryItem(name: "clientName", value: language.clientName),
URLQueryItem(name: "clientName", value: service.clientName),
URLQueryItem(name: "clientVersion", value: self.clientVersion),
URLQueryItem(name: "page", value: url.absoluteString)
]
Expand All @@ -97,8 +97,8 @@ public struct BbcNews {
/// The session to perform network requests from.
private let session: URLSession

/// The language that API results should be localised for.
private let language: Language
/// service: The international service that API results should be localised for e.g. BBC Cymru.
private let service: Service

/// The release track of the API version.
private let releaseTrack: ReleaseTrack?
Expand All @@ -115,13 +115,15 @@ public struct BbcNews {
/// - modelIdentifier: The model identifier of the device e.g. iPhone15,2,
/// - systemName: The name of the operating system e.g. iOS.
/// - systemVersion: The version of the operating system e.g. 16.6.
/// - language: The language that results should be localised for.
/// - service: The international service that API results should be localised for e.g. BBC Cymru.
/// - release: The release track of the API version.
public init(language: Language = .english, release: ReleaseTrack? = nil) {
public init(service: Service = .english, release: ReleaseTrack? = nil) {
self.init(
modelIdentifier: UIDevice.current.modelIdentifier,
systemName: UIDevice.current.systemName,
systemVersion: UIDevice.current.systemVersion
systemVersion: UIDevice.current.systemVersion,
service: service,
release: release
)
}
#endif
Expand All @@ -134,13 +136,13 @@ public struct BbcNews {
/// - modelIdentifier: The model identifier of the device e.g. iPhone15,2,
/// - systemName: The name of the operating system e.g. iOS.
/// - systemVersion: The version of the operating system e.g. 16.6.
/// - language: The language that results should be localised for.
/// - service: The international service that API results should be localised for e.g. BBC Cymru.
/// - release: The release track of the API version.
public init(
modelIdentifier: String,
systemName: String,
systemVersion: String,
language: Language = .english,
service: Service = .english,
release: ReleaseTrack? = nil
) {
let configuration = URLSessionConfiguration.default
Expand All @@ -151,7 +153,7 @@ public struct BbcNews {
]

self.session = URLSession(configuration: configuration)
self.language = language
self.service = service
self.releaseTrack = nil
}

Expand All @@ -173,10 +175,10 @@ public struct BbcNews {
components.host = BbcNews.hostname
components.path = "/fd/abl"
components.queryItems = [
URLQueryItem(name: "clientName", value: self.language.clientName),
URLQueryItem(name: "clientName", value: self.service.clientName),
URLQueryItem(name: "clientVersion", value: BbcNews.clientVersion),
URLQueryItem(name: "page", value: "chrysalis_discovery"),
URLQueryItem(name: "service", value: self.language.service),
URLQueryItem(name: "service", value: self.service.service),
URLQueryItem(name: "type", value: "index")
]

Expand Down Expand Up @@ -242,7 +244,7 @@ public struct BbcNews {
components.host = BbcNews.hostname
components.path = "/fd/abl"
components.queryItems = [
URLQueryItem(name: "clientName", value: self.language.clientName),
URLQueryItem(name: "clientName", value: self.service.clientName),
URLQueryItem(name: "clientVersion", value: BbcNews.clientVersion),
URLQueryItem(name: "page", value: topicId),
URLQueryItem(name: "type", value: "topic")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
//
// Language.swift
// Service.swift
// BbcNews
//
// Created by Bilaal Rashid on 04/06/2024.
//

import Foundation

/// A language supported by the API.
public enum Language: String, Codable, Equatable, Hashable, CaseIterable {
/// The Arabic language.
/// An international service that API results should be localised for e.g. BBC Cymru.
public enum Service: String, Codable, Equatable, Hashable, CaseIterable {
/// The Arabic language service.
case arabic

/// The Cymru language.
/// The Cymru language service.
case cymru

/// The English language.
/// The English language service.
case english

/// The Hindi language.
/// The Hindi language service.
case hindi

/// The Mundo language.
/// The Mundo language service.
case mundo

/// The Russian language.
/// The Russian language service.
case russian

/// The user facing name of the language.
/// The user facing name of the service.
public var displayName: String {
switch self {
case .arabic:
Expand All @@ -45,7 +45,7 @@ public enum Language: String, Codable, Equatable, Hashable, CaseIterable {
}
}

/// The value of the `clientName` URL parameter to use when calling the API for this language.
/// The value of the `clientName` URL parameter to use when calling the API for this service.
public var clientName: String {
switch self {
case .arabic:
Expand All @@ -63,7 +63,7 @@ public enum Language: String, Codable, Equatable, Hashable, CaseIterable {
}
}

/// The value of the `service` URL parameter to use when calling the API for this language.
/// The value of the `service` URL parameter to use when calling the API for this service.
public var service: String {
switch self {
case .arabic:
Expand Down